如何使用laravel显示与产品相关的产品尺寸?

时间:2020-01-17 10:07:49

标签: laravel

我想显示与产品相关的产品尺寸,我已经在数据库中建立了一个黑白表,我只想知道当我单击特定产品时,与该产品相关的尺寸将显示在表格中。内部函数如何使黑白表连接起来,请使用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

控制器

         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>

2 个答案:

答案 0 :(得分:1)

我假设您已经/已经定义了productsproduct_sizes表之间的many-to-many关系

//App\Product.php

public function sizes()
{
    return $this->belongsToMany('App\ProductSize', 'available_product_sizes', 'product_id', 'product_size_id');
}

//App\ProductSize.php
public function products()
{
    return $this->belongsToMany('App\Product', 'available_product_sizes', 'product_size_id', 'product_id');
}

要获得可用的产品尺寸,可以使用:

$sizes = App\Product::find(1)->sizes()->orderBy('size_name')->get();

控制器修复

public function single_product($product_slug) {

    $single_product = Product::with('sizes')->where('product_slug',$product_slug)->first();

    return view('front_end/single_product',compact('single_product'));
}

查看修复

//...
@foreach($single_product->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
//...

答案 1 :(得分:1)

不用雄辩就回答问题

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();

但是强烈建议使用雄辩的关系。