【发布时间】:2017-08-14 11:25:38
【问题描述】:
我正在阅读有关如何在 Laravel 中定义多对多多态关系的教程*,但它没有说明如何使用这种关系保存记录。
在他们的例子中
class Post extends Model
{
/**
* Get all of the tags for the post.
*/
public function tags()
{
return $this->morphToMany('App\Tag', 'taggable');
}
}
和
class Tag extends Model
{
/**
* Get all of the posts that are assigned this tag.
*/
public function posts()
{
return $this->morphedByMany('App\Post', 'taggable');
}
/**
* Get all of the videos that are assigned this tag.
*/
public function videos()
{
return $this->morphedByMany('App\Video', 'taggable');
}
}
我尝试过以不同的方式保存,但对我来说最有意义的尝试是:
$tag = Tag::find(1);
$video = Video::find(1);
$tag->videos()->associate($video);
or
$tag->videos()->sync($video);
这些都不起作用。谁能告诉我我可以尝试什么?
【问题讨论】:
-
错误是什么?
标签: php laravel orm polymorphism