I have 2 table like this
table Base_infos
| id |base_name|
+----+---------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
| 4 | name4 |
| 5 | name5 |
| 6 | name6 |
table Base_types
+----+---------+---------+
| id | base_id| type|
+----+---------+---------+
| 1 | 1 | 1 |
| 2 | 2 | 0 |
| 3 | 3 | 1 |
| 4 | 4 | 0 |
| 5 | 5 | 1 |
| 6 | 6 | 1 |
| 7 | 6 | 0 |
and my query like this
$datas = DB::table('base_infos')
->select('base_infos.id as id', 'base_infos.base_name', 'base_types.type')
->join('base_types', 'base_infos.id', '=', 'base_types.id')
->groupBy('base_infos.id')
->get();
but the data return only first record of table base_types like this
How can I show all type in one line? I'm using Laravel 7 and mysql
Why do you need groupBy('base_infos.id')
? Isn't id unique, if so you don't need that grouping.
Do you manage relations in the model or not?
If you have set the relations in the model, you can use this method to merge / GroupBy between relations
I used this method and it worked
// Displays the most purchased items [ TAMPIL ]
$most_item_buy = Item::with(["item_orders" => function($query) {
$month = date('m');
$year = date('Y');
$query->selectRaw('*, sum(item_order_details.quantity) as total_item');
$query->groupBy('pivot_item_id');
$query->where("order_status", "=", "FINISH");
$query->whereMonth("item_orders.created_at", $bulan);
$query->whereYear("item_orders.created_at", $tahun);
}, "item_images"])
->withTrashed()
->get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.