Skip to content
Advertisement

Laravel Multilayered groupBy

Assume we have this record:

---------------------------------------------------------
|   id  |   relation_id |   color       |   text        |
---------------------------------------------------------
|   1   |       1       |   /* null */  |   text_001    |
---------------------------------------------------------
|   2   |       1       |   /* null */  |   text_002    |
---------------------------------------------------------
|   3   |       2       |   /* null */  |   text_003    |
---------------------------------------------------------
|   4   |       2       |   /* null */  |   text_004    |
---------------------------------------------------------
|   5   |       3       |   /* null */  |   text_005    |
---------------------------------------------------------
|   6   |       3       |   /* null */  |   text_006    |
---------------------------------------------------------
|   7   |       4       |       red     |   text_007    |
---------------------------------------------------------
|   8   |       4       |       red     |   text_008    |
---------------------------------------------------------
|   9   |       4       |       green   |   text_009    |
---------------------------------------------------------
|   10  |       4       |       green   |   text_010    |
---------------------------------------------------------
|   11  |       4       |       blue    |   text_011    |
---------------------------------------------------------
|   12  |       4       |       blue    |   text_012    |
---------------------------------------------------------

While assuming we have this code in our DummyController:

// take note, groupBy() is using a relation here
public function index(Dummy $dummy)
{
    $dummyRecord = $dummy->with('anotherModel')->get()->sortBy('relation_id')->groupBy('anotherModel.column_name');
    return view('index')->with('dummyRecord', $dummyRecord);
}

And lastly, assuming we have @foreach ($dummyRecord as $dummy_column_name => $dummy) inside our index.blade, we could display the result as such:

// assume this is a div
---------------------
|   relation 1      |
---------------------
|       text_001    |
|       text_002    |
---------------------

// assume this is a div
---------------------
|   relation 2      |
---------------------
|       text_003    |
|       text_004    |
---------------------

// assume this is a div
---------------------
|   relation 3      |
---------------------
|       text_005    |
|       text_006    |
---------------------

// assume this is a div
---------------------
|   relation 4      |
---------------------
|       text_007    |
|       text_008    |
|       text_009    |
|       text_010    |
|       text_011    |
|       text_012    |
---------------------

But I want to display the relation 4‘s record of color as a div inside a div, which makes the final output look like this inside the index.blade file:

// assume this is a div
---------------------
|   relation 1      |
---------------------
|       text_001    |
|       text_002    |
---------------------

// assume this is a div
---------------------
|   relation 2      |
---------------------
|       text_003    |
|       text_004    |
---------------------

// assume this is a div
---------------------
|   relation 3      |
---------------------
|       text_005    |
|       text_006    |
---------------------

// assume this is a div
---------------------------------------------
|   relation 4                              |
---------------------------------------------
|                                           |
|   // assume this is a div inside a div    |
|   -----------------                       |
|   |   red         |                       |
|   -----------------                       |
|   |   text_007    |                       |
|   |   text_008    |                       |
|   -----------------                       |
|                                           |
|   // assume this is a div inside a div    |
|   -----------------                       |
|   |   green       |                       |
|   -----------------                       |
|   |   text_009    |                       |
|   |   text_010    |                       |
|   -----------------                       |
|                                           |
|   // assume this is a div inside a div    |
|   -----------------                       |
|   |   blue        |                       |
|   -----------------                       |
|   |   text_011    |                       |
|   |   text_012    |                       |
|   -----------------                       |
|                                           |
---------------------------------------------

Should I use another groupBy to sort the record of colors, or is there another sorting code I could add inside the index.blade file?

Advertisement

Answer

You can group by multiple nested properties like that:

$dummy
    ->with('anotherModel')
    ->get()
    ->sortBy('relation_id')
    ->groupBy(['anotherModel.column_name', 'anotherModel.anotherColumn_name']);

And loop over it in your blade like that:

@foreach(/* ... */)
    <div>
        relation {{ $i }}
        @foreach(/* ... */)
            <div>
                {{ $color }}
                @foreach(/* ... */)
                   {{ $text}}
                @endforeach
            </div>
        @endforeach
    </div>
@endforeach
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement