对于DB中的每个表,我都有四个带有CRUD的刀片。
在homeblade中,我需要一个表,可以在每个表中放一列,然后将其填充到列中,而不是一行中。
这是我在刀片中的代码:
<div class="container">
<table>
<tr>
<th>Companies</th>
<th>Devices</th>
<th>Vehicles</th>
<th>Drivers</th>
</tr>
@foreach($companies as $company)
<tr>
<td>{{$company->name}}</td>
@endforeach
@foreach($devices as $device)
<td>{{$device->type}}</td>
@endforeach
@foreach($vehicles as $vehicle)
<td>{{$vehicle->license_plate}}</td>
@endforeach
@foreach($drivers as $driver)
<td>{{$driver->name}}</td>
@endforeach
</tr>
</table>
</div>
我需要表格来显示这种方式 Picture of table that I need
我为foreach循环和表尝试了几种不同的变体,但是我没有更多的想法了。
答案 0 :(得分:0)
您可以一次foreach
@foreach($companies as $key => $company)
<tr>
<td>{{$company->name}}</td>
<td>{{$devices[$key]->type}}</td>
<td>{{$vehicles[$key]->license_plate}}</td>
<td>{{$drivers[$key]->name}}</td>
</tr>
@endforeach
答案 1 :(得分:0)
这只能通过在新控制器中创建数组来完成。
class WelcomeController extends Controller {
public function index(Request $request) {
$items = [];
foreach (Companies::all() as $comp) $items[]['company']=$comp;
foreach (Device::all() as $index => $dev){
if($items[$index]) $items[$index]['device']=$dev;
else $items[]['device']=$dev;
}
foreach (Vehicle::all() as $index => $veh){
if($items[$index]) $items[$index]['vehicle']=$veh;
else $items[]['vehicle']=$veh;
}
foreach (Driver::all() as $index => $dr){
if($items[$index]) $items[$index]['driver']=$dr;
else $items[]['driver']=$dr;
}
return view('/welcome', ['items'=>$items]);
}
在刀片的表主体中,我这样写:
<tbody>
@foreach($items as $company)
<tr>
<td>{{isset($company['company'])?$company['company']->name:''}}</td>
<td>{{isset($company['device'])?$company['device']->type:''}}</td>
<td>{{isset($company['vehicle'])?$company['vehicle']->type:''}}</td>
<td>{{isset($company['driver'])?$company['driver']->name:''}}</td>
</tr>
@endforeach
</tbody>