问题
Let's say I have 2 table in database
In laravel I used query builder like this
DB::table('chart')->join('chart_detail', 'chart.id', '=', 'chart_detail.id_cart')->get();
The result is something like this:
It's not what I want to display data, because too much redundancy
I want to display data like this:
Where I'm wrong in join sql command?
回答1:
This is not something you can do with your query exactly... on your front-end you can group it together by doing something like this (better to use a customer id if you have one):
$data = [];
foreach ($result as $row) {
if (!isset($data[ $row['nama_customer'] ])) {
$data[ $row['nama_customer'] ] = [];
}
$data[ $row['nama_customer'] ][] = $row;
}
var_dump($data);
Then when you render your HTML you have enough information to group it together by setting rowspan on your <tr>
tag.
来源:https://stackoverflow.com/questions/34521949/join-to-display-spesific-data-in-query-builder-laravel