Laravel 在 1 个标题下显示同一日期的所有职位

我创建了一个表格,将日期显示为标题,然后在其下方显示当天的工作。但是,当我在同一天有两项工作时,它会在每项工作下方显示两次日期。我想在 1 个日期(标题)下一起显示当天的所有作业。

<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>


狐的传说
浏览 126回答 3
3回答

ITMISS

在控制器中:    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');}    在刀片中:     <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>

BIG阳

最好的方法是首先对您的集合进行排序,以确保每个日期都有自己的负载:$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 dateforeach($dates->toArray() as $key => $date)&nbsp; &nbsp; $dates[$date] = $loads->filter(function($load){&nbsp; &nbsp; &nbsp; &nbsp; return $load->pickup_date === $date;&nbsp; &nbsp; });// return the dates instead of loadsreturn $dates;现在来说说刀片部分:<table>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; @foreach($dates as $date => $collection)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td&nbsp; colspan="8" class="ml-1 h4 text-warning">{{date('l, d-F-Y', strtotime($date))}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach($collection as $load)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{date("g:i a", strtotime($load->pick_up_time))}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><textarea>{{$user->customer}}</textarea></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$load->pickup_location}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$load->drop_off_location}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<!-- <td>{{$load->distance}}</td>-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!--<td><textarea>{{$load->description}}</textarea></td>-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!--<td>{{$load->customer_reference}}</td>-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$load->vehicle}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$load->driver}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><textarea>{{$load->notes}}</textarea></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-pencil-square-o" style="color:#FF6F00;"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-truck" style="color:#5cac00;"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-phone" style="color:#00b1e6;"></i></a></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; </tbody></table>

扬帆大鱼

我建议将日期保存在循环内,然后在渲染标题之前检查它是否与上一个相同;我不熟悉 Laravel,正在寻找一种方法来做到这一点,但看起来显然不鼓励在模板本身中设置局部变量。您需要做的基本上是按日期(或者在本例中,按标题)对结果进行分组,这样当您遍历循环时,您可以在同一日期拥有多行。这是一个非常粗略的示例,但应该可以让您更好地了解我的建议:$loads = array(&nbsp; &nbsp; array( 'pickup_date' => 'foo', /* more items here... */ ),&nbsp; &nbsp; array( 'pickup_date' => 'foo', /* more items here... */ ),&nbsp; &nbsp; array( 'pickup_date' => 'bar', /* more items here... */ ),&nbsp; &nbsp; array( 'pickup_date' => 'bar', /* more items here... */ ),&nbsp; &nbsp; array( 'pickup_date' => 'baz', /* more items here... */ ),);// Group by date...$grouped_loads = array_reduce($loads, function($carry, $load) {&nbsp; &nbsp; $carry[ $load['pickup_date'] ][]= $load;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return $carry;}, array());此时,$grouped_loads应该如下所示:array(&nbsp; &nbsp; 'foo' => array(&nbsp; &nbsp; &nbsp; &nbsp; array( 'pickup_date' => 'foo', /* more items here... */ ),&nbsp; &nbsp; &nbsp; &nbsp; array( 'pickup_date' => 'foo', /* more items here... */ ),&nbsp; &nbsp; ),&nbsp; &nbsp; 'bar' => array(&nbsp; &nbsp; &nbsp; &nbsp; array( 'pickup_date' => 'bar', /* more items here... */ ),&nbsp; &nbsp; &nbsp; &nbsp; array( 'pickup_date' => 'bar', /* more items here... */ ),&nbsp; &nbsp; ),&nbsp; &nbsp; 'baz' => array(&nbsp; &nbsp; &nbsp; &nbsp; array( 'pickup_date' => 'baz', /* more items here... */ ),&nbsp; &nbsp; ),);因此,您现在将执行一个嵌套操作foreach,每个日期一个,然后日期中的每个项目一个。Laravel 为您提供了 和$loop,$loop->first因此您现在只能渲染第一项的标题:@foreach($grouped_loads as $loads)&nbsp; &nbsp; @foreach($loads as $load)&nbsp; &nbsp; &nbsp; &nbsp; @if ($loop->first)&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td&nbsp; colspan="8" class="ml-1 h4 text-warning">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{date('l, d-F-Y', strtotime($load->pickup_date))}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{date("g:i a", strtotime($load->pick_up_time))}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><textarea>{{$user->customer}}</textarea></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$load->pickup_location}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$load->drop_off_location}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- <td>{{$load->distance}}</td>-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!--<td><textarea>{{$load->description}}</textarea></td>-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!--<td>{{$load->customer_reference}}</td>-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$load->vehicle}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$load->driver}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><textarea>{{$load->notes}}</textarea></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-pencil-square-o" style="color:#FF6F00;"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-truck" style="color:#5cac00;"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="{{route('EditLoad',$load->id)}}" class="fa fa-2x fa-phone" style="color:#00b1e6;"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; @endforeach@endforeach
打开App,查看更多内容
随时随地看视频慕课网APP