Laravel displaying all jobs on same date under 1 header

Viewed 60

I created table displaying the date as a heading then the job for that day beneath it. But when i have 2 jobs on the same day it displays the date twice with each job beneath it. I want to display all the jobs for that day together under 1 Date (Heading). thankshow its displaying

<table>
    <tbody>
        @foreach($loads as $load)
            <tr><td  colspan="8" class="ml-1 h4 text-warning">{{date('l, d-F-Y', strtotime($load->pickup_date))}}</td></tr>
            <tr>
                <td>{{date("g:i a", strtotime($load->pick_up_time))}}</td>
                <td><textarea>{{$user->customer}}</textarea></td>
                <td>{{$load->pickup_location}}</td>
                <td>{{$load->drop_off_location}}</td>
               <!-- <td>{{$load->distance}}</td>-->
                <!--<td><textarea>{{$load->description}}</textarea></td>-->
                <!--<td>{{$load->customer_reference}}</td>-->
                <td>{{$load->vehicle}}</td>
                <td>{{$load->driver}}</td>
                <td><textarea>{{$load->notes}}</textarea></td>
                <td>
                    <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-pencil-square-o" style="color:#FF6F00;"></i></a>
                    <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-truck" style="color:#5cac00;"></i></a>
                    <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-phone" style="color:#00b1e6;"></i></a></td>
                </td>
            </tr>
        @endforeach
    </tbody>
</table>
3 Answers

I was going to recommend saving the date inside the loop and then before rendering the header check if it was the same as the last one; I am not familiar with Laravel and was looking for a way to do this, but looks like setting a local variable in the template itself is apparently discouraged.

What you need to do is basically group your results by date (or in this case, by the header), that way when you are walking the loop, you could have multiple rows on the same date.

This is a very rough example but should give you a better idea of what I am proposing:

$loads = array(
    array( 'pickup_date' => 'foo', /* more items here... */ ),
    array( 'pickup_date' => 'foo', /* more items here... */ ),
    array( 'pickup_date' => 'bar', /* more items here... */ ),
    array( 'pickup_date' => 'bar', /* more items here... */ ),
    array( 'pickup_date' => 'baz', /* more items here... */ ),
);

// Group by date...
$grouped_loads = array_reduce($loads, function($carry, $load) {
    $carry[ $load['pickup_date'] ][]= $load;
    
    return $carry;
}, array());

At this point, $grouped_loads should look like this:

array(
    'foo' => array(
        array( 'pickup_date' => 'foo', /* more items here... */ ),
        array( 'pickup_date' => 'foo', /* more items here... */ ),
    ),
    'bar' => array(
        array( 'pickup_date' => 'bar', /* more items here... */ ),
        array( 'pickup_date' => 'bar', /* more items here... */ ),
    ),
    'baz' => array(
        array( 'pickup_date' => 'baz', /* more items here... */ ),
    ),
);

So you will now do a nested foreach, one for each date, then one for each item in the date. Laravel provides you with $loop and $loop->first, so you can now only render the header for the first item:

@foreach($grouped_loads as $loads)
    @foreach($loads as $load)
        @if ($loop->first)
        <tr>
            <td  colspan="8" class="ml-1 h4 text-warning">
                {{date('l, d-F-Y', strtotime($load->pickup_date))}}
            </td>
        </tr>
        @endif
        <tr>
            <td>{{date("g:i a", strtotime($load->pick_up_time))}}</td>
            <td><textarea>{{$user->customer}}</textarea></td>
            <td>{{$load->pickup_location}}</td>
            <td>{{$load->drop_off_location}}</td>
            <!-- <td>{{$load->distance}}</td>-->
            <!--<td><textarea>{{$load->description}}</textarea></td>-->
            <!--<td>{{$load->customer_reference}}</td>-->
            <td>{{$load->vehicle}}</td>
            <td>{{$load->driver}}</td>
            <td><textarea>{{$load->notes}}</textarea></td>
            <td>
                <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-pencil-square-o" style="color:#FF6F00;"></i></a>
                <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-truck" style="color:#5cac00;"></i></a>
                <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-phone" style="color:#00b1e6;"></i></a>
            </td>
        </tr>
    @endforeach
@endforeach

Best way is to sort your collection in the first place to make sure each date has its own loads:

$loads = {....}; // here are the loads
$dates = []; // here will be the dates

// get all loads pickup dates
$dates = $loads->pluck('pickup_date');

// remove duplicated dates
$dates = $dates->unique();

// assign each load to its date
foreach($dates->toArray() as $key => $date)
    $dates[$date] = $loads->filter(function($load){
        return $load->pickup_date === $date;
    });

// return the dates instead of loads
return $dates;

Now for the blade part:

<table>
    <tbody>
        @foreach($dates as $date => $collection)
            <tr>
                <td  colspan="8" class="ml-1 h4 text-warning">{{date('l, d-F-Y', strtotime($date))}}</td>
            </tr>
            @foreach($collection as $load)
                <tr>
                    <td>{{date("g:i a", strtotime($load->pick_up_time))}}</td>
                    <td><textarea>{{$user->customer}}</textarea></td>
                    <td>{{$load->pickup_location}}</td>
                    <td>{{$load->drop_off_location}}</td>
                   <!-- <td>{{$load->distance}}</td>-->
                    <!--<td><textarea>{{$load->description}}</textarea></td>-->
                    <!--<td>{{$load->customer_reference}}</td>-->
                    <td>{{$load->vehicle}}</td>
                    <td>{{$load->driver}}</td>
                    <td><textarea>{{$load->notes}}</textarea></td>
                    <td>
                        <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-pencil-square-o" style="color:#FF6F00;"></i></a>
                        <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-truck" style="color:#5cac00;"></i></a>
                        <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-phone" style="color:#00b1e6;"></i></a></td>
                    </td>
                </tr>
            @endforeach
        @endforeach
    </tbody>
</table>

thank you guys , your responses really helped - it got me to the solution :)

in controller:

    public function store(Request $request) {



    $originalDate = $request['pickup'];
    $PickUpDate = date("Y-m-d", strtotime($originalDate));

    $originalDate = $request['destination'];
    $DestinationDate = date("Y-m-d", strtotime($originalDate));
    
     $originalDate = $request['createdDate'];
    $createdDate = date("Y-m-d", strtotime($originalDate));

    $load = new Load;

    $load->user_id = Auth::id();
    $load->customer = $request['customer'];
    $load->pickup_date = $PickUpDate;
    $load->destination_date = $DestinationDate;
    $load->creation_date = $createdDate;
    $load->trailer = $request->trailer;
    $load->pickup_location = $request['pickup_location'];
    $load->drop_off_location = $request['drop_off_location'];
    $load->distance = $request['distance'];
    $load->description = $request['description'];
    $load->quantity = 1;
    $load->customer_reference = $request['customer_reference'];
    $load->load_number = $request['load_number'];
    $load->additional_details = $request['additional_details'];
    $load->vehicle = $request['vehicle'];
    $load->driver = $request['driver'];
    $load->notes = $request['notes'];
    $load->Rate = $request['rate'];
    $load->pick_up_time = $request['pickupTime'];
    $load->destination_time = $request['destinationTime'];
     $load->creation_time = $request['createdTime'];
    $load->consignee = $request['consignee'];
    $load->save();

    return back()->with('Success', 'Load created successfully');
}    

In Blade:

     <tbody> dd($loads);
                      @foreach($loads as $key=>$new_load) 

                        <tr><td  colspan="8" class="ml-1 h4 text-warning">{{date('l, d-F-Y', strtotime($key))}}</td></tr>
            
                         @foreach($new_load as $load)
                            <tr>
                                <td>{{date("g:i a", strtotime($load->pick_up_time))}}</td>
                                <td>{{$load->customer}}</td>
                                <td>{{$load->pickup_location}}</td>
                                <td>{{$load->drop_off_location}}</td>
                               <!-- <td>{{$load->distance}}</td>-->
                                <!--<td><textarea>{{$load->description}}</textarea></td>-->
                                <!--<td>{{$load->customer_reference}}</td>-->
                                <td>{{$load->vehicle}}</td>
                               <!-- <td>{{$load->trailer}}</td>-->
                                <td>{{$load->driver}}</td>
                                <td><textarea>{{$load->notes}}</textarea></td>
                                <td>
                                    <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-pencil-square-o" style="color:#FF6F00;"></i></a>
                                    <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-truck" style="color:#5cac00;"></i></a>
                                    <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-phone" style="color:#00b1e6;"></i></a></td>
                                </td>
                            </tr>
  

                        @endforeach
                    @endforeach
             </tbody>
    
Related