doufan9395
2017-09-21 11:03
浏览 58
已采纳

多个表的Laravel Eloquent关系问题

In Laravel 5.5 I try to create a small application to manage products of a couple of sellers/stores.

Therefore, I have four different models like this:

Seller.php

class Attribute extends Model
{

    public function items()
    {

        return $this->belongsToMany(Item::class);
    }
}

Item.php

class Item extends Model
{

    public function seller()
    {

         return $this->belongsTo(Seller::class);
    }

    public function category()
    {

        return $this->belongsTo(Category::class);
    }

    public function attributes()
    {

        return $this->belongsToMany(Item::class);
    }
}

Category.php

class Category extends Model
{

    public function items()
    {

        return $this->hasMany(Item::class);
    }
}

Attribute.php

class Attribute extends Model
{

    public function items()
    {

        return $this->belongsToMany(Item::class);
    }
 }

For the many-to-many relation between Attributes & Items, I created a pivot table:

Schema::create('attribute_item', function (Blueprint $table) {
    $table->integer('attribute_id')->unsigned()->index();
    $table->foreign('attribute_id')->references('id')->on('attributes')->onDelete('cascade');
    $table->integer('item_id')->unsigned()->index();
    $table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
    $table->primary(['attribute_id', 'item_id']);
});

The goal of the entire application is to:

  • fetch all items of a seller by category with attributes (for filtering or something)
  • Fetch a specific item of an seller and get its attributes and category

I'm a little bit confused by Laravels relationhip methods and which one to use in that case.

May it is better to hasManyThrough or polymorphic relationhips?

I have to admit that I have a little logic problem here. Hopefully you can help me.

Thank you!

图片转代码服务由CSDN问答提供 功能建议

在Laravel 5.5中,我尝试创建一个小应用程序来管理几个卖家/商店的产品。</ p >

因此,我有四种不同的模型:</ p>

Seller.php </ p>

  class Attribute extends Model  
 {
 
公共函数项目()
 {
 
返回$ this-&gt; belongsToMany(Item :: class); 
} 
} 
 </ code> </ pre> \  n 
 

Item.php </ p>

  class Item extends Model 
 {
 
 public function seller()
 {
 
返回$ this-  &gt; belongsTo(Seller :: class); 
} 
 
公共函数类别()
 {
 
返回$ this-&gt; belongsTo(Category :: class); 
} 
 
  public function attributes()
 {
 
返回$ this-&gt; belongsToMany(Item :: class); 
} 
} 
 </ code> </ pre> 
 
 

类别 .php </ p>

 类类别扩展Model 
 {
 
公共函数项目()
 {
 
返回$ this-&gt; hasMany(Item ::  class); 
} 
} 
 </ code> </ pre> 
 
 

Attribute.php </ p>

  class属性扩展Model 
 {
 
公共函数项()
 {
 
返回$ this-&gt; belongsToMany(Item :: class); 
} 
  } 
 </ code> </ pre> 
 
 

对于属性和属性之间的多对多关系 项目,我创建了一个数据透视表:</ p>

  Schema :: create('attribute_item',function(Blueprint $ table){
 $ table-&gt; integer('attribute_id')  ) - &gt; unsigned() - &gt; index(); 
 $ table-&gt; foreign('attribute_id') - &gt; references('id') - &gt; on('attributes') - &gt; onDelete(  'cascade'); 
 $ table-&gt;整数('item_id') - &gt; unsigned() - &gt; index(); 
 $ table-&gt; foreign('item_id') - &gt; references('  id') - &gt; on('items') - &gt; onDelete('cascade'); 
 $ table-&gt; primary(['attribute_id','item_id']); 
}); 
 <  / code> </ pre> 
 
 

整个申请的目标是:</ p>

  • 按类别提取卖家的所有商品( </ li>
  • 获取卖家的特定商品并获取其属性和类别</ li> </ ul>

    我有一点点 Laravels关系方法和在这种情况下使用的方法相混淆。</ p>

    hasManyThrough </ code>或多态关系可能更好吗?</ p>

    我必须承认我有一点兴趣 这里有gic问题。 希望你能帮帮我。</ p>

    谢谢!</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题