Laravel-多对多多态关系 [英] Laravel - Many-to-Many polymorphic relationships

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

问题描述

我要反对Laravel 5.7中的多态关系定义

I'm banging my head against a polymorphic relationship definition within Laravel 5.7

以下是数据情况: 我有一个用户模型,一个产品模型和一个商品模型 我基本上是想为我的用户建立一个愿望清单,它既可以包含商品,也可以包含产品,并且我希望在那里具有多态关系,因为以后我将不得不添加新的商品类型来出售.

Here's the data situation: I have a model for Users, a model for Products and a model for Merchandising I basically want to build a WishList for my user, that can contain both Merchandises and Products, and I want to have a polymorphic relationship there because I will have to add new types of things to sell at a later time.

在我的(简化的)当前数据模式中,我有

In my (simplified) current data schema, I have

users
-email
-id

products
-id
-name

merchandises
-id
-name

臭名昭著的人

wish_list_items
-user_id
-wish_list_item_type
-wish_list_id

关于关系(在用户方面):

In terms of relationships (on User):

public function wishListProducts()
{
    return $this->morphedByMany(Product::class, 'wish_list_items');
}

public function wishListMerchandises()
{
    return $this->morphedByMany(Merchandise::class, 'wish_list_items');
}

现在这很好用.但这还不足以实现我想要实现的目标-我想念的是,无论其类型如何,都需要获取愿望清单"中所有项目的步骤.目前:

Now this working great. But it is not enough for what I want to achieve - what I am missing is the step to fetch ALL the items of the Wish List, regardless of their type. At the moment:

public function wishListAll()
{
    return $this->load(['wishListProducts','wishListMerchandises']);
}

显然可以给我所有东西的清单,但是要放在单独的收藏夹中,我想要一个统一的收藏夹.

Which obviously give me a list of everything, but in separated collections - and I want a unified one.

第一个问题,是否有一种方法可以获取与该用户相关联的所有信息,而无论其类型如何?这可能很简单,但我在任何地方都找不到.

First question, is there a way to get everything associated to that user, regardless of the type? It might be pretty simple but I can't find it anywhere.

如果这是不可能的(我实际上怀疑-但这意味着这些多态关系不过是避免创建另一个数据透视表的一种方式,我不介意这样做),人们认为会是什么最好的进行方式? 最初的想法是创建一个接口,我的所有可售商品模型都将实现该接口,以统一所有内容,尽管如此,我仍然不得不遍历所有分离的集合,除非有一种简单的方法可以将它们合并,因为它们实现相同界面? 还是我应该为WishListItems定义一个模型(目前是数据透视表,因此尚未定义该模型),让我所有的可售商品模型扩展超级模型,并尝试将关系链接在一起?

If it's not possible (which I actually suspect - but that would mean that those polymorphic relationships are not much more than a way to avoid creating another pivot table, which I wouldn't mind doing), what do people think would be the best way to proceed? The starting idea was to create an interface, which all my sellables model would implement, in order to unify everything - I'd still have to loop through all my seperated collections though, unless there's an easy way to merge them as they implement the same interface? Or should I just define a model for my WishListItems (it's a pivot table at the moment, so the model hasn't been defined), make all my sellables models extand the super model, and try to link relationships together?

感谢您的输入!

推荐答案

默认"解决方案是合并两个(或更多)集合的访问器:

The "default" solution is an accessor that merges the two (or more) collections:

public function getWishListItemsAttribute()
{
    return $this->wishListProducts->toBase()->merge($this->wishListMerchandises);
}

// $user->wishListItems

+++更新+++

我创建了一个用于使用视图合并关系的程序包:
https://github.com/staudenmeir/laravel-merged-relations

I've created a package for merging relationships using views:
https://github.com/staudenmeir/laravel-merged-relations

首先,在迁移中创建合并视图:

First, create the merge view in a migration:

use Staudenmeir\LaravelMergedRelations\Facades\Schema;

Schema::createMergeView(
    'wish_list_items', [(new User)->wishListMerchandises(), (new User)->wishListProducts()]
);

然后定义关系:

class User extends Model
{
    use \Staudenmeir\LaravelMergedRelations\Eloquent\HasMergedRelationships;

    public function wishListItems()
    {
        return $this->mergedRelation('wish_list_items');
    }
}

像其他任何关系一样使用它:

Use it like any other relationship:

$user->wishListItems;

$user->wishListItems()->paginate();

User::with('wishListItems')->get();

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

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