Laravel Eloquent:多对多的逆(多态)
Laravel Eloquent: Inverse of Many To Many (Polymorphic)
从官方Laravel文档复制的示例:
例如,Post 模型和视频模型可以共享与标签模型的多态关系。在这种情况下使用 many-to-many 多态关系将允许您的应用程序具有单个 table 可能与帖子或视频相关联的唯一标签。首先,让我们检查一下建立这种关系所需的 table 结构:
posts
id - integer
name - string
videos
id - integer
name - string
tags
id - integer
name - string
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
从标签 object 我想获取该标签所属的所有视频和帖子(如果是 morphOne 和 morphMany 我可以通过 morphTo() 方法做到这一点)
Laravel 说,我需要在 Tag 模型中定义视频和帖子方法,以便定义一个逆函数,但我想要一个像 taggables 这样的关系,它将 return 受人尊敬的 parent(无论是 Post 还是视频)
我需要类似imageable的东西(但它是一对一的多态,我需要多对多的这种东西)
您可以在枢轴模型中使用 MorphOne
/MorphMany
。
https://laravel.com/docs/8.x/eloquent-relationships#defining-custom-intermediate-table-models
class Video extends Model
{
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable')->using(Taggable::class);
}
public function taggables()
{
return $this->morphMany(Taggable::class, 'taggable');
}
}
class Post extends Model
{
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable')->using(Taggable::class);
}
public function taggables()
{
return $this->morphMany(Taggable::class, 'taggable');
}
}
class Tag extends Model
{
public function posts()
{
return $this->morphedByMany(Post::class, 'taggable')->using(Taggable::class);
}
public function videos()
{
return $this->morphedByMany(Video::class, 'taggable')->using(Taggable::class);
}
public function taggables()
{
return $this->hasMany(Taggable::class/*, 'tag_id'*/)
}
}
use Illuminate\Database\Eloquent\Relations\MorphPivot;
class Taggable extends MorphPivot
{
public $incrementing = false; // this is the default value. Change if you need to.
public $guarded = []; // this is the default value. Change if you need to.
protected $table = 'taggables';
public function taggable()
{
return $this->morphTo();
}
public function tag()
{
return $this->belongsTo(Tag::class/*, 'tag_id'*/);
}
}
从官方Laravel文档复制的示例:
例如,Post 模型和视频模型可以共享与标签模型的多态关系。在这种情况下使用 many-to-many 多态关系将允许您的应用程序具有单个 table 可能与帖子或视频相关联的唯一标签。首先,让我们检查一下建立这种关系所需的 table 结构:
posts
id - integer
name - string
videos
id - integer
name - string
tags
id - integer
name - string
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
从标签 object 我想获取该标签所属的所有视频和帖子(如果是 morphOne 和 morphMany 我可以通过 morphTo() 方法做到这一点)
Laravel 说,我需要在 Tag 模型中定义视频和帖子方法,以便定义一个逆函数,但我想要一个像 taggables 这样的关系,它将 return 受人尊敬的 parent(无论是 Post 还是视频)
我需要类似imageable的东西(但它是一对一的多态,我需要多对多的这种东西)
您可以在枢轴模型中使用 MorphOne
/MorphMany
。
https://laravel.com/docs/8.x/eloquent-relationships#defining-custom-intermediate-table-models
class Video extends Model
{
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable')->using(Taggable::class);
}
public function taggables()
{
return $this->morphMany(Taggable::class, 'taggable');
}
}
class Post extends Model
{
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable')->using(Taggable::class);
}
public function taggables()
{
return $this->morphMany(Taggable::class, 'taggable');
}
}
class Tag extends Model
{
public function posts()
{
return $this->morphedByMany(Post::class, 'taggable')->using(Taggable::class);
}
public function videos()
{
return $this->morphedByMany(Video::class, 'taggable')->using(Taggable::class);
}
public function taggables()
{
return $this->hasMany(Taggable::class/*, 'tag_id'*/)
}
}
use Illuminate\Database\Eloquent\Relations\MorphPivot;
class Taggable extends MorphPivot
{
public $incrementing = false; // this is the default value. Change if you need to.
public $guarded = []; // this is the default value. Change if you need to.
protected $table = 'taggables';
public function taggable()
{
return $this->morphTo();
}
public function tag()
{
return $this->belongsTo(Tag::class/*, 'tag_id'*/);
}
}