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!

  • 写回答

1条回答 默认 最新

  • dpdx51205 2017-09-21 13:27
    关注

    You can use whereHas method to find the nested relationship let's for example your first goal

    fetch all items from a seller by category with attributes (for filtering or something)

    You may write the following:

    $items = Item::whereHas('seller.items', function ($query) {
            $query->whereHas('categories', function ($categories) {
                $categories->where('name', '=', 'Mens');
            })
            ->orWhereHas('attributes', function ($attributes) {
                $attriutes->where('size', '=', 'large');
            });
        })->get();
    

    Find out more about this: https://laravel.com/docs/5.5/eloquent-relationships#querying-relationship-existence

    This will give you the list of items if you want to get items with categories and attributes you can use with method to get the relational data:

    $items = Item::whereHas('seller.items', function ($query) {
            $query->whereHas('categories', function ($caegories) {
                $categories->where('name', '=', 'Mens');
            })
            ->orWhereHas('attributes', function ($attributes) {
                $atributes->where('size', '=', 'large');
            });
        })
        ->with('categories', 'attributes')
        ->get();
    

    Hope this guide you the problem which you are facing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么