我想显示与产品相关的产品尺寸,我已经在数据库中建立了黑白表,我只想知道当我单击特定产品时,与该产品相关的尺寸将显示在表格中。函数如何使黑白表连接起来,请使用DB类制作连接表,因为对我来说这很容易理解。
有人有想法吗?
please check product sizes image https://i.stack.imgur.com/Yt6Jk.png
数据库
product_sizes table ---------------------------------------------------- id | size_name | Body_length | Body_width | ---------------------------------------------------- 1 | M | 2.3 | 3.4 2 | L | .5 | 4.4 3 | s | 2.5 | 3.2 4 | xs | 3.5 | 2.4 products table ---------------------------------------- id | product_name | Description | ---------------------------------------- 1 Ultraclub 1 | Ultraclub 300 | 2 Ultraclub 2 | Ultraclub 200 | 3 Ultraclub 3 | Ultraclub 500 | 4 Ultraclub 4 | Ultraclub 600 | 5 Ultraclub 5 | Ultraclub 400 | available_product_sizes table ---------------------------------------- id | product_id | product_size_id ---------------------------------------- 1 | 2 | 1 2 | 3 | 4 3 | 4 | 3 4 | 1 | 2 5 | 2 | 3
Controller
public function single_product($product_slug){ $Sizes=DB::table('product_sizes')->get(); $single_product=DB::table('products')->where('product_slug',$product_slug)->get(); return view('front_end/single_product',compact('single_product','Sizes')); }
html视图
<div class="product-tabs__pane product-tabs__pane--active" id="chart">
<table class="table table-bordered text-center">
<tr style="background-color:#3366cc;color:white">
<td><b>Sizes</b>
</td>
<td><b>Body length</b>
</td>
<td><b>Body width</b>
</td>
<td><b>Sleeve length</b>
</td>
</tr>
@foreach($Sizes as $size)
<tr>
<td><b>{{$size->sizes_name}}</b>
</td>
<td>{{$size->Body_length}}</td>
<td>{{$size->Body_width}}</td>
<td>{{$size->Sleeve_length}}</td>
</tr>
@endforeach
</table>
</div>
我想显示与产品相关的产品尺寸,我已经在数据库中建立了一个黑白表,我只想知道当我单击特定产品时,与该产品相关的尺寸...
use Illuminate\Support\Facades\DB; //include this
$products = DB::table('products as p')
->join('available_product_sizes as aps','aps.product_id','p.id')
->join('product_sizes ps','ps.id','aps.product_size_id')
->where('product_slug',$product_slug)->get();