如何解决Laravel雄辩的foreach
我正在尝试在每个驱动程序的计划文件的刀片文件中创建单独的表。我正在尝试通过控制计算机中的驱动器来实现这一目标。我可以在控制器中为每个驱动程序设置一个单独的查询,但是如果我们添加或删除驱动程序,这将需要大量维护。
请帮助我了解如何完成此操作-以下是我目前拥有的内容:
控制器
"Trucking::DRIVERS" is the constant in my model with the names of the drivers.
public function overview()
{
$drivers = Trucking::DRIVERS;
foreach($drivers as $driver) {
$truckings = Trucking::query()
->where('truck',$driver)
->where('active',1)
->where('status','scheduled')
->orderBy('trucking_date_requested')
->orderBy('order')
->get();
}
return view('trucking.overview',compact('driver','truckings'));
}
刀片文件:
@foreach($driver as $table)
<div class="flex-col space-y-4">
<x-table>
<x-slot name="head">
<x-table.heading>Date</x-table.heading>
<x-table.heading>Order</x-table.heading>
<x-table.heading>Status</x-table.heading>
<x-table.heading>Type</x-table.heading>
<x-table.heading>Model</x-table.heading>
<x-table.heading>SN</x-table.heading>
<x-table.heading>From</x-table.heading>
<x-table.heading>City</x-table.heading>
<x-table.heading>To</x-table.heading>
<x-table.heading>City</x-table.heading>
</x-slot>
<x-slot name="body">
@foreach($truckings as $trucking)
<x-table.row
class="odd:bg-gray-100 hover:bg-yellow-100"
title="{{ $trucking->note }}"
>
<x-table.cell>{{ $trucking->trucking_date_requested }}</x-table.cell>
<x-table.cell>{{ $trucking->order }}</x-table.cell>
<x-table.cell>
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full {{ App\Models\Trucking::SORTING[$trucking->sorting] }}">
{{ $trucking->status }}
</span>
</x-table.cell>
<x-table.cell>{{ $trucking->type }}</x-table.cell>
<x-table.cell>{{ $trucking->model }}</x-table.cell>
<x-table.cell>{{ $trucking->sn }}</x-table.cell>
<x-table.cell>{{ $trucking->from_name }}</x-table.cell>
<x-table.cell>{{ $trucking->from_city }}</x-table.cell>
<x-table.cell>{{ $trucking->to_name }}</x-table.cell>
<x-table.cell>{{ $trucking->to_city }}</x-table.cell>
</x-table.row>
@endforeach
</x-slot>
</x-table>
</div>
@endforeach
谢谢您的帮助!
解决方法
在您的控制器文件中,您可以创建一个自定义数组,将驱动程序及其卡车推入该数组。它应该看起来像这样:
$data = []
$drivers = Trucking::DRIVERS;
foreach($drivers as $driver) {
$truckings = Trucking::query()
->where('truck',$driver)
->where('active',1)
->where('status','scheduled')
->orderBy('trucking_date_requested')
->orderBy('order')
->get();
array_push($data,[
'driver' => $driver,'truckings' => $truckings,])
}
return view('trucking.overview')->with(['data' => $data]);
现在您可以在刀片中实际使用foreach数据变量,基于$data['driver']
渲染表(这将是驱动程序对象),然后使用$data['truckings']
渲染驱动程序的货运(这将是运输对象的集合)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。