Laravel多态关系命名似乎不一致

时间:2020-07-16 17:58:00

标签: php laravel

参考文档,一对多关系表示为

class Comment extends Model
{
    /**
     * Get the owning commentable model.
     */
    public function commentable()
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    /**
     * Get all of the post's comments.
     */
    public function comments()
    {
        return $this->morphMany('App\Comment', 'commentable');
    }
}

class Video extends Model
{
    /**
     * Get all of the video's comments.
     */
    public function comments()
    {
        return $this->morphMany('App\Comment', 'commentable');
    }
}

其中多对多关系表示为

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');
    }
}

但是,由于(morphable_type,morphable_id)列是指“帖子/视频”,因此在一对多的注释中使用morphTo()和在{{1 }}。我希望morphToMany()Post中有morphToMany()

是否有一个名称为什么会被翻转的原因,因为在我看来,就像在PHP内置函数中一样,needle和haystacks在各种函数中切换参数位置而没有明显的原因-这是我记得的东西,但是它对我来说没有意义。

谢谢!

0 个答案:

没有答案
相关问题