Join to display spesific data in query builder laravel

▼魔方 西西 提交于 2019-12-11 00:14:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!