i want get something like list of news with number of comment
article ------------- comment
news1 ---------------- 10
news2 ---------------- 2
news3 ---------------- 13
news4 ---------------- 25
i know how to do it with raw sql but can't make it with laravel. laravel can handle raw query but i don't want to use it for the whole query.
here is my current snipped code:
$newslist = News::where('status_id', 2)->orderBy('id', 'desc')->skip(0)->take($itemPerLoad)
->join('msuser', 'msuser.id', '=', 'news.user_id')
->select(....)->get();
i have tried put count on select method but got error (with join comment table as not written above)
->select('news.id', 'count(mscomment.id) as commented', ...)
because i can't join it, currently i use separate code to count it and set it to json response
$commented = Comment::where('news_id', $news->id)->count();
i think its not good way to get the number because it will request count for each loop
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire