简体   繁体   English

Laravel 双多态关系

[英]Laravel double polymorphic relation

I have the three tables articles , locations and companies .我有articleslocationscompanies这三个表。 One row of each table can be linked with another row form one of the other tables over a fourth table calles links , where links looks like:每个表的一行可以通过第四个表链接与其他表之一的另一行links ,其中links看起来像:

  • id ID
  • from_id from_id
  • from_type from_type
  • to_id to_id
  • to_type输入

For example, the article with the ID 1 could have a connection to the locations ID 1:例如,ID 为 1 的article可能与 ID 为 1 的locations有关联:

  • id: 1编号:1
  • from_id: 1 from_id: 1
  • from_type: App\Models\Article from_type:应用\模型\文章
  • to_id: 1 to_id: 1
  • to_type: App\Models\Location to_type:应用\模型\位置

and the a connection with the company 3:以及与公司 3 的联系:

  • id: 2编号:2
  • from_id: 1 from_id: 1
  • from_type: App\Models\Article from_type:应用\模型\文章
  • to_id: 3 to_id: 3
  • to_type: App\Models\Company to_type:应用\模型\公司

Which laravel relations would I need here?我在这里需要哪种 laravel 关系? Its some kind of polymorphic many to many relation?它是某种多态的多对多关系?

Actually this works with the links table实际上这适用于链接表


class Article extends Model
{
    use HasFactory;

    public function companies()
    {
        return $this->morphToMany(Company::class, 'to', 'links', 'from_id', 'to_id', 'id', 'id', 'to');
    }

    public function locations()
    {
        return $this->morphToMany(Location::class, 'to', 'links', 'from_id', 'to_id', 'id', 'id', 'to');
    }
}

Following test data in links table以下链接表中的测试数据在此处输入图像描述

This query这个查询

Article::with(['companies', 'locations'])->first();

//output
App\Models\Article {#2010
     id: 1,
     title: "Et molestiae consequuntur harum provident officia neque.",
     created_at: "2020-12-07 20:18:42",
     updated_at: "2020-12-07 20:18:42",
     companies: Illuminate\Database\Eloquent\Collection {#2003
       all: [
         App\Models\Company {#2007
           id: 3,
           name: "Ledner-Schumm",
           created_at: "2020-12-07 20:18:42",
           updated_at: "2020-12-07 20:18:42",
           pivot: Illuminate\Database\Eloquent\Relations\MorphPivot {#2004
             from_id: 1,
             to_id: 3,
             to_type: "App\Models\Company",
           },
         },
       ],
     },
     locations: Illuminate\Database\Eloquent\Collection {#1999
       all: [
         App\Models\Location {#1997
           id: 2,
           name: "Maiamouth",
           created_at: "2020-12-07 20:19:08",
           updated_at: "2020-12-07 20:19:08",
           pivot: Illuminate\Database\Eloquent\Relations\MorphPivot {#2001
             from_id: 1,
             to_id: 2,
             to_type: "App\Models\Location",
           },
         },
       ],
     },
   }

And for the inverse relation而对于反比关系


class Company extends Model
{
    use HasFactory;

    public function articles()
    {
        return $this->morphedByMany(Article::class, 'from', 'links', 'to_id', 'from_id', 'id', 'id');
    }
}

Query询问

Company::with('articles')->find(3);

//Output
App\Models\Company {#2025
     id: 3,
     name: "Ledner-Schumm",
     created_at: "2020-12-07 20:18:42",
     updated_at: "2020-12-07 20:18:42",
     articles: Illuminate\Database\Eloquent\Collection {#2026
       all: [
         App\Models\Article {#2024
           id: 1,
           title: "Et molestiae consequuntur harum provident officia neque.",
           created_at: "2020-12-07 20:18:42",
           updated_at: "2020-12-07 20:18:42",
           pivot: Illuminate\Database\Eloquent\Relations\MorphPivot {#2049
             to_id: 3,
             from_id: 1,
             from_type: "App\Models\Article",
           },
         },
       ],
     },
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM