簡體   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