Neversaysblack Neversaysblack - 4 years ago 183
SQL Question

Join to display spesific data in query builder laravel

Let's say I have 2 table in database

enter image description here
enter image description here

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:

enter image description here

It's not what I want to display data, because too much redundancy

I want to display data like this:

enter image description here

Where I'm wrong in join sql command?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download