如何根据Laravel中的ID匹配从3个不同的表中获得最佳结果。

时间:2016-06-02 00:04:29

标签: laravel blade

尝试为特定供应商提供产品和变体。 我可以很容易地获得产品,但无法弄清楚如何最好地匹配带有匹配product_id的变体并将其发送到视图。 Variants.product_id与Product.ID匹配

这有效(为供应商提供产品)

 public function suppliers($id) {

    $supplier = Supplier::orderby('company_name', 'ASC')->find($id);
    $products = Supplier::find($id)->products;

    $data = [];
    $data['supplier'] = $supplier;
    $data['products'] = $products;

    return view('admin.purchasing.supplier-details', $data);
}

我试过这个以获得变种也没有运气。

控制器:

    public function suppliers($id) {

    $supplier = Supplier::orderby('company_name', 'ASC')->find($id);
    $products = Supplier::find($id)->products;
    $variants = array();
    foreach ($products as $product) {
        $product_id = $product->id;
        $variants[] = Variant::find($product_id);
    }
    $data = [];
    $data['supplier'] = $supplier;
    $data['products'] = $products;
    $data['variants'] = $variants;
    return view('admin.purchasing.supplier-details', $data);
}

查看:

    @foreach($products as $product)
        <tr>
            <td>{{ $product['title'] }}</td>
            @foreach($variants as $variant)
                @if($variant->product_id == $product['id'])
                    <td>${{ $variant->price }}</td>   
                @else
                    <td>not set</td>
                @endif
            @endforeach    
        </tr>
    @endforeach

任何提示非常感谢。

1 个答案:

答案 0 :(得分:1)

首先,您应该在模型上设置关系以使其正常工作

像这样的例子:

Supplier.php

public function products()
{
    return $this->hasMany('App\Product');
}

<强> Product.php

 public function variants()
{
    return $this->hasMany('App\Variant');
}
public function Supplier()
{
    return $this->belongsToMany('App\Supplier'); //in case you have only one supplier for each product change this to belongsto
}

<强> Variant.php

public function products()
    {
        return $this->belongsToMany('App\Product'); //not sure if this should be manytomany or one to many , it deppends on what you did
    }

现在你可以做到这一点

<强>控制器

public function suppliers($id) {

    $Data = Supplier::where('id',$id)->orderby('company_name', 'ASC')->with('products.variants')->first(); //you will get the supplier with all products associated to him with variants foreach product

    return view('admin.purchasing.supplier-details')->with('Data',$Data); // now you just loop the $supplierwithproducts->products to get results (dd the variable to check output)

    }

查看

{{ $Data->name }} // supplier name since Supplier model was the starting point
@foreach($Data->products as $product) //loop all products related to that supplier
  {{ $product->name }} //shows product name (depends on you database collumns
    @foreach($product->variants as $variant) // loops all variants in the current product
      {{ $variant->name }} // shows variants
    @endforeach
@endforeach

如果您复制并粘贴此代码,它可能无效,但这会让您了解如何处理laravel中的关系(levrage eloquent relations)

检查此信息以获取更多信息

Laravel Docs

Laracasts Defining Relationships With Eloquent

Laracasts Updating Records and Eager Loading