I am trying to get all slots, then group them by room. Within my view, I want to have a row of the room:
<tr>
<td>Room ID 1</td>
<td>Slot ID 1</td>
<td>Slot ID 2</td>
<td>Slot ID 3</td>
.................
</tr>
<tr>
<td>Room ID 2</td>
<td>Slot ID 1</td>
<td>Slot ID 2</td>
<td>Slot ID 3</td>
.................
</tr>
I believe I am close, but not sure of the "Laravel way". The data looks good - as in, the query is getting/grouping correctly, I think my struggle is understanding how to loop through the collection.
MyController.php
public function routineViewDayWise()
{
$sunday_routines = Routine::where('day_id', 1)->orderBy('room_id')->get()->groupBy('room_id');
return view('day_wise_routine', compact('sunday_routines'));
}
In my view I can see I am getting this:
MyView.blade.php
#items: array:4 [▼
2 => IlluminateDatabaseEloquentCollection {#1323 ▼
#items: array:2 [▼
0 => AppModelsRoutine {#1339 ?}
1 => AppModelsRoutine {#1340 ?}
]
}
4 => IlluminateDatabaseEloquentCollection {#1322 ?}
6 => IlluminateDatabaseEloquentCollection {#1336 ?}
8 => IlluminateDatabaseEloquentCollection {#1335 ?}]
SOLUTION
Here is what my view looks like to get the output I wanted.
MyView.blade.php
@foreach($sunday_routines as $room_id => $routines)
<tr>
<td>{{ $room_id }}</td>
@foreach($routines as $routine)
<td>{{ $routine->slot_id }}</td>
@endforeach
</tr>@endforeach
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…