3 回答
TA贡献1871条经验 获得超8个赞
在控制器中:
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>
TA贡献1859条经验 获得超6个赞
最好的方法是首先对您的集合进行排序,以确保每个日期都有自己的负载:
$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;
现在来说说刀片部分:
<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>
TA贡献1799条经验 获得超9个赞
我建议将日期保存在循环内,然后在渲染标题之前检查它是否与上一个相同;我不熟悉 Laravel,正在寻找一种方法来做到这一点,但看起来显然不鼓励在模板本身中设置局部变量。
您需要做的基本上是按日期(或者在本例中,按标题)对结果进行分组,这样当您遍历循环时,您可以在同一日期拥有多行。
这是一个非常粗略的示例,但应该可以让您更好地了解我的建议:
$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());
此时,$grouped_loads应该如下所示:
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... */ ),
),
);
因此,您现在将执行一个嵌套操作foreach,每个日期一个,然后日期中的每个项目一个。Laravel 为您提供了 和$loop,$loop->first因此您现在只能渲染第一项的标题:
@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
- 3 回答
- 0 关注
- 125 浏览
添加回答
举报