I have been working on a personal project where users can rate different shops. Among many, I have two tables schemas that I am working on.
Table: Shops
- id
- shop_name
- user_id (shops belongs to a user)
Table : Ratings
- id
- shop_id (foreign_key)
- rating
- user_id
Now I want to fetch all the shops and its corresponding ratings using SQL join.
$shop = Shop::leftJoin('ratings', 'ratings.shop_id', '=', 'shops.id')
->select('shops.*', 'ratings.rating')->get();
return response($shop);
When I do this I get response but the it is repeated for each ratings on the same shop.
[
{
"id": 1,
"name": "manjil",
"rating": 4
},
{
"id": 1,
"name": "manjil",
"rating": 5
},
{
"id": 1,
"name": "manjil",
"rating": 2
}]
What I really want is :
[{
"id": 1,
"name": "manjil",
"rating": [4,5,2]
}]
Please Laravel geeks, help :)
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire