Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ouzinehamza's avatar

multi group with group name

Hello guys, i have a situation. I would really love to get some help. thanks in advance. I am working on a project in which i give the user the ability to export a report in pdf. i want the exported data to be collected in a table with a row mentioned the group name example: Date : 01-01-2023 => { 'status: Active'=> { data of active status of precized date. } 'status: pending'=> { data of pending status of precized date. } }; Date : 02-01-2023 ={... } and so one Any help i am stack.

0 likes
13 replies
ouzinehamza's avatar

$data = Model::where('user_id', auth()->user()->id) ->whereBetween('date', [$from, $to]) ->orderBy('date', 'ASC') ->orderBy('time', 'ASC') ->get()->groupBy('date')->orderBy('status'); here is the query

webrobert's avatar

Apologies I am o my phone and I still can’t read your code. ` `` use back ticks to open and close code. Blocks.

I think this is what you’re after. It’s an example snippet I made for another answer A query …

$reservationsSlots = Reservation::where('status', 1)
	->whereBetween('reservation_date', [$start_date, $end_date])
	->get()
    ->groupBy('reservation_time');

and for the blade file... use pad() to fill empty reservations lots 😉

<table>
    <tr>
        <th>Time</th>
        <th>Table No</th>
        <th>Room</th>
        <th>Guest Name</th>
        <th>Adult</th>
        <th>Kids</th>
        <th>Meal Plan</th>
        <th>Taken by</th>
        <th>Updated by</th>
        <th>Booking</th>
        <th>Booking time</th>
        <th>Remarks</th>
    </tr>

@foreach($reservationSlots as $reservations)
    @foreach($reservations->pad(5, null) as $reservation)
    <tr>
        <td>{{ $reservation->reservation_time ?? '' }}</td>
        <td>{{ $reservation->table_no ?? '' }}</td>
        <td>{{ $reservation->room ?? '' }}</td>
        <td>{{ $reservation->guest_name ?? '' }}</td>
        <td>{{ $reservation->adult ?? '' }}</td>
        <td>{{ $reservation->kids ?? '' }}</td>
        <td>{{ $reservation->meal_plan ?? '' }}</td>
        <td>{{ $reservation->taken_by ?? '' }}</td>
        <td>{{ $reservation->updated_by ?? '' }}</td>
        <td>{{ $reservation->booking_date ?? '' }}</td>
        <td>{{ $reservation->booking_time ?? '' }}</td>
        <td>{{ $reservation->remarks ?? '' }}</td>
    </tr>
    @endforeach
@endforeach
</table>
ouzinehamza's avatar

thank you so much for your answer $data = Model::where('user_id', auth()->user()->id) ->whereBetween('date', [$from, $to]) ->orderBy('date', 'ASC') ->orderBy('time', 'ASC') ->get()->groupBy('date')->orderBy('status'); here is the query

ouzinehamza's avatar

i want each group to have a groub by inside of it with it's group title

ouzinehamza's avatar

result must be groupedby date 01-01-2023 for example and inside of it it must be groupedby status 'active ' for example or 'pending'

webrobert's avatar

@ouzinehamza

Use

@foreach($grouped as $models) // first foreach
    @if ($loop->first)
        This is the first iteration.
        {{ $models->first()->status }}
    @endif

// second foreach is here
webrobert's avatar

You could also use that inside the second loop and not have to use first just depend on the html and preference.

ouzinehamza's avatar

thank you so much for you solution. but it didn't work or maybe it's me who doesn't know how to use it. could you please explain a bit more??

Please or to participate in this conversation.