Answer the question
In order to leave comments, you need to log in
How to get comments to the news and at the same time the author of the comment?
How to get comments to the news and at the same time the author of the comment?
/* Получение одной новости */
public static function getIDNews($newsID){
$getIDNews = News::with(['comments', 'authorComments'])
->where('id_news', '=', $newsID)
->first();
return $getIDNews;
}
/* Получение авторов комментариев */
public function authorComments(){
return $this->belongsTo(User::class, 'author_comments', 'id_users');
}
/* Получение всех комментариев к новости */
public function comments(){
return $this->hasMany(Comments::class, 'news_comments', 'id_news');
}
Answer the question
In order to leave comments, you need to log in
I decided not to worry too much and wrote the following code:
/* Получение комментариев и их авторов */
public static function getCommentsIDNews($newsID){
$getCommentsIDNews = Comments::where([
['news_comments', '=', $newsID],
['status_comments', '=', 1]
])
->join('users', 'author_comments', '=', 'id_users')
->get();
return $getCommentsIDNews;
}
I'm not from the Laravel world, but I see a reference to the User model, in this model, most likely, the field is not id_users, but id
krch. show the `users` table again
, maybe there is no such user in the `users` table at all
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question