如何获取文章所有者user_id并保存到notifiable_id字段Suppos $ articlecomment = $ article_owner_id
我的代码:
$articlecomment = new Article_comment();
$articlecomment->user_id = Auth::user()->id;//Comment by user id
$articlecomment->article_id = $request->articleid;
$articlecomment->comment = $request->comment;
$articlecomment->save();
auth()->user()->notify(new ArticleNotification($articlecomment));
//$articlecomment->user()->notify(new ArticleNotification($articlecomment));
数据库截图我想在notifible_id字段enter image description here上找到article_owner_user_id
如果您为Article_comment
和Article
模型建立了关系,则可以通过该关系访问“文章所有者”。
例如,在Article_comment
类中定义“Article”模型的关系(假设Article
是模型的名称):
class Article_comment extends Model {
....
public function article() {
return $this->hasOne('App\Article', 'id', 'article_id')
}
....
}
一旦你有了这个集合,就可以像这样访问你的关系(和后续属性)(假设article_owner_id
是你的Article
模型的属性):
$articlecomment->article->article_owner_id
编辑:
您呼叫通知的用户将通知该用户。因此,要通知文章所有者,您需要获取文章的用户并从该实例(而不是auth用户)调用notify。如果您在Article类上设置了与用户的关系,您只需从中调用notify,或者从article_owner_id
获取用户并调用notify。
例:
$user = User::where('id', '=', $articlecomment->article->article_owner_id)->first();
$user->notify(new ArticleNotification($articlecomment));
通过在Article类上设置关系,您可以改为调用notify,如下所示:
$articlecomment->article->user->notify(new ArticleNotification($articlecomment));
有关雄辩关系的更多信息,请参阅https://laravel.com/docs/5.6/eloquent-relationships#introduction
Solved
$articlecomment->save();
$article = Article::where('id','=',$request->articleid)->first();
if($article->user_id != Auth::User()->id){
$article->user->notify(new ArticleNotification($article));
}