【发布时间】:2017-11-17 16:39:09
【问题描述】:
我在从我的 Laravel 集合中编写一些嵌套的 HTML Tables 时遇到了一些麻烦。
我的模特
public static function offers($userId)
{
return DB::table('users_vehicles')
->join('dealer_offers', 'users_vehicles.id', '=', 'dealer_offers.vehicle_id')
->where('user_id', '=', $userId)
->select('users_vehicles.*', 'dealer_offers.*');
}
我的控制器
public function dash()
{
$userCars = UserVehicles::offers(Auth::user()->id)->get();
return view('customer.dash', compact('userCars'));
}
我的收藏结果
在我的视图中刀片模板
@foreach( $userCars->chunk(1) as $userCarChunk)
<div id="no-more-tables">
<table class="table table-bordered table-hover">
<caption>Offers for My Mazda 326 </caption>
<thead>
<tr>
<th class="numeric">DEALER RATING</th>
<th class="numeric">AMOUNT </th>
<th class="numeric"> 3 DAYS To Accept </th>
</tr>
</thead>
<tbody>
@foreach( $userCarChunk as $car)
<tr>
<td data-title="First Name">
<div class="rating">
<label>
<input type="radio" name="rating" value="5" title="5 stars"> 5
</label>
</div>
</td>
<td data-title="Last Name"> 55 000 </td>
<td data-title="Username">
<a href="{{ route('customer.eval.show', 1) }}">Accept</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endforeach
前端视图
问题
如何为所有用户车辆创建一个嵌套的 HTML 表,以便我可以写下该特定车辆的车辆名称和所有报价?
我希望我的问题很清楚。
【问题讨论】:
-
你能分享你的表结构吗?乍一看,您似乎可以通过使用雄辩的关系而不是使用
DB::table('users_vehicles')来解决您的问题(这是您的代码出现问题的明确迹象)。 -
表结构。不是 ORM 模型。我将假设您按照关系顺序有表
user_info <--> user <--> user_vehicles <--> vehicle_offers <--> dealer?如果是这种情况,那么您应该执行$iteratee = UserVehicles::with("vehicle_offers")->whereHas("vehicle_offers")->where("user_id",auth()->id())并为每辆车打印车辆,然后打印相关报价表。任何地方都不需要静态的东西。 -
You mean This 另外,我现在试试你的建议
标签: php laravel laravel-5 laravel-5.4