繁体   English   中英

Laravel雄辩的查询使用两个关系

[英]Laravel eloquent query using two relations

我有下一个数据库结构-产品可以在许多类别中,产品可以在许多市场中。 创建模型\\App\\Product\\App\\Market\\App\\Category并具有多对多关系belongsToMany()

在此处输入图片说明

class Product extends Model
{
    public function categories()
    {
        return $this->belongsToMany('App\Category');
    }

    public function markets()
    {
        return $this->belongsToMany('App\Market');
    }
}

class Category extends Model
{
    public function products()
    {
        return $this->belongsToMany('App\Product');
    }
}

class Market extends Model
{
    public function products()
    {
        return $this->belongsToMany('App\Product');
    }
}

route.web我获得了展示产品的类别

Route::get('/catalog/{current_category?}', 'CatalogController@index')->name('catalog.index');

我可以从会话中获得的当前市场(用户在打开网站时选择的市场)

$market = $request->session()->get('market'); // or Session::get('market');
// $market->id
// $market->slug

在我的MarketController@index我想从路线中获取所有类别的产品,并从会话中获取当前市场的所有产品。 但是我该怎么办呢? 我可以得到分类产品和市场产品。 但是,如何同时获得类别和市场产品?

public function index(Request $request, Category $current_category = null)
{
    if ($current_category) {

        $market_id = $request->session()->get('market')->id;

        $products = $current_category->products;
        // ...
    }
}

如果要基于category的产品,请使用以下查询:

$products = $current_category->products()->get();

如果要基于市场的产品,首先需要获得市场对象,而不是基于市场的产品。

$market =  Market::find($market_id);
$market_products = $market->products()->get();

如果要基于市场和类别的产品,可以在下面的查询中使用。

$products = Product::whereHas('categories', function($q) {
                        $q->where('category_id', $current_category->id);
                     })
                     ->whereHas('markets', function($q) {
                        $q->where('market_id', $market_id);
                     })
                     ->get();

正如评论中指出的那样,您可以通过多对多态关系来实现它

  • 表结构
categories
    id - integer
    name - string

markets
    id - integer
    name - string

products
    id - integer
    name - string

productables
    product_id - integer
    productable_id - integer
    productable_type - string
  • 类别模型
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    /**
     * Get all of the products for the category.
     */
    public function products()
    {
        return $this->morphToMany('App\Product', 'productable');
    }
}
  • 市场模式
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Market extends Model
{
    /**
     * Get all of the products for the market.
     */
    public function products()
    {
        return $this->morphToMany('App\Product', 'productable');
    }
}
  • 产品型号
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    /**
     * Get all of the categories that are assigned this product.
     */
    public function categories()
    {
        return $this->morphedByMany('App\Category', 'productable');
    }

    /**
     * Get all of the markets that are assigned this product.
     */
    public function markets()
    {
        return $this->morphedByMany('App\Market', 'productable');
    }
}

比您可以同时获得属于(某些类别和某些市场)的产品

$products = \App\Product::where(['productable_id' => $category->id, 'productable_type' => get_class($category)])->orWhere(['productable_id' => $market->id, 'productable_type' => get_class($market)])->get();

假设从您的问题中知道类别和市场。

@YasinPatel的解决方案也应该起作用,但是这样,您的数据库体系结构将更加灵活。 现在由您决定。 研究多态关系解,您会发现它很有趣。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM