I am learning Laravel and some difficulties have arisen that I could not solve.
I get the data from the database with such a query, something like records here.
DB::table('shots AS s')->join('users AS u','u.id','=','s.author_id')-> select('u.picture','u.username','s.picture AS s_picture','s.title','s.id AS shot_id')-> where('u.id',$id)->limit($limit)-> get(); Everything works, but I still need to get the number of "likes" to these posts and I do not understand how I can do this with this request.
I tried to use count in select , but either worked incorrectly or received an error.
Here is the likes tableiduser_idshot_id
Attempting via DB :: select (I’m accustomed to it) returns one record, I understand that I’m doing something wrong - but I don’t understand what exactly ..
DB::select("SELECT u.username AS username,u.picture AS picture,s.title AS title,s.picture AS s_picture,count(l.shot_id) AS likes FROM users AS u INNER JOIN shots AS s JOIN likes AS l ON u.id=s.author_id WHERE s.id = $id AND s.id=l.shot_id LIMIT $limit");
likestable and return the number of likes - Baldsqlquery:select s.id, l.countLikes from shots as s join users u on s.author_id=u.id join (select shot_id, Count(user_id) as countLikes from likes group by shot_id ) as l on s.id = l.shot_id- Baldleft join, it will be much better to get the necessary information at a time, at least it will be faster - Bald