Laravel 中与数据透视表的多态关系?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16102144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:43:09  来源:igfitidea点击:

Polymorphic relationships with pivot table in Laravel?

laravelhas-and-belongs-to-manylaravel-4polymorphism

提问by Andreas Franzén

I got 3 models, Article, Building, Person.

我有 3 个模型,文章、建筑、人物。

  1. These models need to reference each other in a few ways. For example the building needs to be able to reference collections of Person like $building->architects(), $building->owners(), a article might reference collections of Person with $article->authors() and Person might reference collections of Building like $person->owned_buildings()

  2. Each model should have a function like "references" to get a collection of mixed models.

  1. 这些模型需要以几种方式相互引用。例如,建筑物需要能够引用像 $building->architects()、$building->owners() 这样的 Person 集合,一篇文章可能会通过 $article->authors() 引用 Person 的集合,而 Person 可能会引用集合像 $person->owned_buildings() 这样的建筑

  2. 每个模型都应该有一个类似于“引用”的函数来获取混合模型的集合。

I'm thinking that this should be possible with something like:

我认为这应该可以通过以下方式实现:

class Article extends Eloquent {
    public function referenceable()
    {
        return $this->morphTo();
    }

    public function authors()
    {
        return $this->morphMany('Person', 'referenceable');
    }
}

class Building extends Eloquent {
    public function referenceable()
    {
        return $this->morphTo();
    }

    public function architects()
    {
        return $this->morphMany('Person', 'referenceable');
    }
}

class Person extends Eloquent {
    public function referenceable()
    {
        return $this->morphTo();
    }

    public function owned_buildings()
    {
        return $this->morphMany('Building', 'referenceable');
    }
}

So the question is what would the pivot table look like?

所以问题是数据透视表会是什么样子?

回答by BigBlueHat

You can define a belongsTousing a morphMany-stylerelationship by adding a whereto the mix:

您可以通过在混合中添加 a来定义belongsTo使用morphMany-style关系where

  public function followers() {
    return $this
      ->belongsToMany('User', 'follows', 'followable_id', 'user_id')
      ->where('followable_type', '=', 'Thing');
  }

The wherewill simply be sure Eloquent doesn't mismatch IDs.

where将只是确保 Eloquent 不会与 ID 不匹配。

Hope that helps!

希望有帮助!

回答by BenG

Polymorphic relationships are basically 1-to-many relationships. They allow you to reuse a model on many other models.

多态关系基本上是一对多的关系。它们允许您在许多其他模型上重用模型。

For instance if Post has many images, and a User might have many avatars, you can use the same image models without conflict. So instead of setting an image with a user_id field and a post_id field, you can use a generic imageable_id.

例如,如果 Post 有很多图像,而一个 User 可能有很多头像,那么您可以使用相同的图像模型而不会发生冲突。因此,您可以使用通用的 imageable_id,而不是使用 user_id 字段和 post_id 字段设置图像。

You require a many-to-many relationship which is not currently supported with Eloquent's morphMany()

Eloquent 的 morphMany() 当前不支持多对多关系

You can do several types of pivot tables for that. For instance set up two architect/building and owner /building pivot tables separately with building_id and person_id fields. Or you could setup a single pivot table with an extra 'type' field to define the role

您可以为此执行多种类型的数据透视表。例如,分别使用 building_id 和 person_id 字段设置两个建筑师/建筑和所有者/建筑数据透视表。或者您可以设置一个带有额外“类型”字段的数据透视表来定义角色