多个表的Laravel雄辩关系问题 [英] Laravel Eloquent relation issue for multiple tables

查看:51
本文介绍了多个表的Laravel雄辩关系问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

Category.php

class Category extends Model
{

    public function items()
    {

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

Attribute.php

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:

  • 按属性获取卖家的所有商品(用于过滤等)
  • 获取卖家的特定商品并获取其属性和类别

我对Laravels关系型方法有些困惑,在那种情况下该使用哪种方法.

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

使用hasManyThrough或多态关联词可能更好吗?

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.

谢谢!

推荐答案

您可以使用whereHas方法来查找嵌套关系,例如,我们的第一个目标

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)

您可以编写以下内容:

$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();

详细了解以下内容: https://laravel.com /docs/5.5/eloquent-relationships#querying-relationship-existence

如果要获取具有类别和属性的项目,可以使用with方法获取关系数据,这将为您提供项目列表:

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.

这篇关于多个表的Laravel雄辩关系问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆