我在这里再次请求你的帮助。
我想将分支产品与数量分组,但是,我无法将产品名称显示为产品名称。到现在为止,它显示的是产品id而不是产品名称
+-----------------+ +-----------------+ +----------------+
| product table | | quantity table | | branch table |
+-----------------+ +-----------------+ +----------------+
| id | | prod_id | | id |
| productname | | branch_id | | branchname |
+-----------------+ | quantity | +----------------+
+-----------------+
分支模型
public function products()
{
return $this->hasMany('App\Quantity', 'branch_id');
}
数量模型
public function branch()
{
return $this->belongsTO('App\Branch', 'branch_id', 'id');
}
产品型号
public function productquantities()
{
return $this->hasMany('App\Quantity','prod_id');
}
我的观点
@foreach($dataBranch as $Branch)
<h1>Branch {{$Branch->branch_name}}</h1>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th> </th>
<th>Product Name</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
@forelse($Branch->products as $Product)
<tr>
<td align="center" style="text-align:center"> <a href="#" class="avatar"><img src="{{ asset('productimg') }}/" width="100px"/> </a> </td>
<td> <a class="name">{{$Product->prod_id}} //this should be the product name instead of product id</a></td>
<td><em class="productprice">{{$Product->quantity}}</em> </td>
</tr>
@empty
<tr><td colspan='4'><em>No Data</em></td></tr>
@endforelse
</tbody>
</table>
@endforeach
我的控制器
$dataBranch = Branch::with('products')->get();
enter code here
1条答案
按热度按时间xwmevbvl1#
从现在起,您将使用数量表而不是产品表连接分支。您可以在分支模型中执行以下操作(未测试)。这个想法是通过数量从分支到产品的连接。
然而,我看到的一个问题是,如果您这样做,您可能会得到一个关于分支和产品id字段的不明确列的sql警告。我会在products表id=>product\u id和branch表id=>branch\u id中重命名。在quantity表中,将branch\u id重命名为类似branch\u表id的内容。
然后在您的分支和产品模型中: