Watch the free series. Laravel 8 from scratch
Feb 26, 2023
4
Level 1
Probably Simple Query to be done
I am still learning. At the moment though I just need what I consider a simple.
Single Table:
task
sign
leg_size
Sample Data would be:
1111
5050
pole
1111
5151
tpost
2222
4234
pole
I want to display all the data as such:
task #: 1111
-- Sign #: 5050
-- Leg Size: Pole
-- Sign #: 5151
-- Leg Size: tpost
task #: 2222
-- Sign #: 4234
-- Leg Size: pole
Currently Tried:
DB::table('signs')->select('task','sign','leg_size')->groupBy('task')->get();
It does not get all the sign numbers grouped by task from what I can see. I also had to disable strict mode which I rather not if possible.
Thinking I will have to do a query to get all the unique task numbers. Then filter on that information some how to create what I need to loop over in the blade file.
Level 27
Do the grouping in the resulting collection instead of in the database:
// Controller
$data = DB::table('signs')
->select('task', 'sign', 'leg_size')
->get()
->groupBy('task')
;
// Blade template
@foreach ($data as $task => $details)
<h1>{{ $task }}</h1>
@foreach ($details as $detail)
<div>Sign: {{ $detail['sign'] }}</div>
<div>Leg size: {{ $detail['leg_size'] }}</div>
@endforeach
@endforeach
Please or to participate in this conversation.