簡體   English   中英

hasManyThrough laravel關系

[英]hasManyThrough laravel relation

我在下面有三個表。

+-----------------+   +---------------------+   +-------------+ 
|    products     |   |  product_countries  |   |  countries  |
+-----------------+   +---------------------+   +-------------+ 
|   id            |   |  id                 |   |   id        |
|   name          |   |  product_id         |   |   name      |
|   description   |   |  country_code       |   |   code      |
+-----------------+   |  other columns      |   +-------------+
                      +---------------------+

在Country :: class模型中具有hasManyThrough來獲取所有產品。

public function products()
{
    return $this->hasManyThrough(
        'App\Product',
        'App\ProductCountry',
        'product_id', // Foreign key on feeds table...
        'id', // Foreign key on articles table...
        'code', // Local key on users table...
        'country_code' // Local key on feeds table...
    );
}

我想要與某個國家或一個國家相關的產品:

$user = \App\Models\Country::find(1);
dd($user->products);

您所描述的最佳關系非常適合很多人,通常在Laravel中將其寫為雙方的“ belongsToMany ”。

class Product extends Model
{
    public function countries()
    {
        return $this->belongsToMany(Country::class);
    }
}

class Country extends Model
{
    public function products()
    {
        return $this->belongsToMany(Product::class);
    }
}

如果您遵循Laravel的命名約定:

  • product_countries重命名為country_product
  • country_product使用country_id代替country_code

...您應該能夠以您期望的方式訪問這些關系:

$country = \App\Models\Country::find(1);
dd($country->products);

$countries = \App\Models\Country::with('products')->where(...)->get();
foreach ($countries as $country) {
    dd($country->products);
}

如果不想遵守約定,則可以自定義定義關系的方式。 有關更多詳細信息,請參見Laravel的文檔


對於您的情況,要使用自定義表名和自定義字段名指定關系,您可以執行以下操作:

// In your Product model
return $this->belongsToMany(Country::class, 'product_countries', 'country_code', 'product_id');

// In your Country model
return $this->belongsToMany(Product::class, 'product_countries', 'product_id', 'country_code');

多對多是正確的模型關系。 如果不是這樣,則需要在第二個參數中添加自定義表名。Eloquent會自動按字母順序為您創建(例如country_product )。 您無需創建ProductCountry Model,因為它只是一個數據透視表。

您沒有發布遷移文件(只是通過猜測),建議您使用country_id但是如果您使用country_code / product_id作為外鍵,則還需要將其添加到參數以及遷移文件中,例如(還要檢查官方文檔

 $table->foreign('product_id')->references('id')->on('products');
 $table->foreign('country_code')->references('code')->on('countries');

試試這個...

在您的產品模型中

class Product extends Model
{
   public function countries()
   {
      return $this->belongsToMany('App\Country','product_countries','product_id','country_code');
   }
}

在您的國家/地區模型中

class Country extends Model
{
   public function products()
   {
      return $this->belongsToMany('App\Product','product_countries','country_code','product_id');
   }
}

在您的控制器中

use App\Country;

$products = Country::find(1)->products()->get(); //finding by id or

$products = Country::where('country_code',$value)->with('products')->get(); //finding by code

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM