laravel 5.6:鉴于每个产品都有多个品牌,如何在表格中显示每个产品和可用品牌
我是laravel的新手,我正在尝试开发一个应用程序,该应用程序有一个部分,其中有一个表格,列出了每个产品、可用品牌、可用数量和销售价格。每个产品都有多个品牌 我有三张桌子: 第一个表项目\主表:此表存储产品名称、购买和销售价格 第二个表库存数量:此表根据店铺的产品id和店铺id存储产品数量 第三个表项品牌:此表根据产品id和店铺id存储每个产品的品牌 这是密码 获取产品名称、类别和物料数量的主查询laravel 5.6:鉴于每个产品都有多个品牌,如何在表格中显示每个产品和可用品牌,laravel,laravel-5.6,Laravel,Laravel 5.6,我是laravel的新手,我正在尝试开发一个应用程序,该应用程序有一个部分,其中有一个表格,列出了每个产品、可用品牌、可用数量和销售价格。每个产品都有多个品牌 我有三张桌子: 第一个表项目\主表:此表存储产品名称、购买和销售价格 第二个表库存数量:此表根据店铺的产品id和店铺id存储产品数量 第三个表项品牌:此表根据产品id和店铺id存储每个产品的品牌 这是密码 获取产品名称、类别和物料数量的主查询 $details=items_master::select('items_masters.id
$details=items_master::select('items_masters.id as
product_id','items_masters.name as item_name','items_masters.category','buying_price','selling_price','item_quantities.product_id','item_quantities.quantity','categories.id','categories.serial')
->join('item_quantities','items_masters.id','=','item_quantities.item_id')
->join('categories','categories.id','=','items_masters.category')
->where([['item_quantities.shop_id', '=',$shop]] )
->groupBY('product_id')
->get();
查询以获取产品ID
$product_ids = collect($details)->pluck('product_id');
查询以获取产品品牌
$brands = item_brands::whereIn('pid', $product_ids)
->where('shop_id', $shop)
->where('status', 0)
->get();
视图中的代码
<div class="table-responsive">
<table class="table table-bordered table-striped" >
<thead>
<tr>
<th>Item Name</th>
<th>Item Brands</th>
<th>Stock Qty</th>
<th>Category</th>
<th>Price</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach($details as $detail)
<tr>
<td>{{$detail->item_name}} </td>
<td><div class="form-group">
@foreach($brands as $brand )
<span>{{$brand->name}}</span>
@endforeach
</div>
</td>
<td>{{$detail->quantity.' '.$detail->units}} </td>
<td >
<td>{{$detail->category}} </td>
<td >
<b>Ksh. {{$detail->selling_price}}</b>
</td>
</tr>
@endforeach
</tbody>
</table>
项目名称
商品品牌
库存数量
类别
价格
删除
@foreach($details作为$detail)
{{$detail->item_name}
@foreach($brands作为$brand)
{{$brand->name}
@endforeach
{{$detail->quantity.'.$detail->units}
{{$detail->category}
Ksh。{{$detail->售价}
@endforeach
根据上面的代码,我得到了表中所有产品的所有品牌。我需要每个产品只在其行上显示其品牌,例如
产品:平板电脑|品牌:三星、苹果
产品:台式机|品牌:惠普、联想
目前,这就是出现的情况
产品:平板电脑|品牌:三星、苹果、惠普、联想
产品:台式机|品牌:三星、苹果、惠普、联想您是否尝试过使用收集方法来限制foreach中使用的品牌?即:
@foreach($brands->where('pid', $detail->product_id) as $brand )
<span>{{$brand->name}}</span>
@endforeach
@foreach($brands->where('pid',$detail->product_id)作为$brand)
{{$brand->name}
@endforeach
由于每个产品都有所有品牌,因此您只需为当前的$detail
选择品牌即可
编辑:但老实说,我觉得所有3个查询都可以写成一个有说服力的查询,但我必须看到所有3个模型都有关系