小编And*_*zén的帖子

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

我有3个模型,文章,建筑,人.

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

  2. 每个模型都应该具有类似"参考"的功能,以获得混合模型的集合.

我认为这应该是可能的,例如:

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

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

polymorphism has-and-belongs-to-many laravel laravel-4

3
推荐指数
1
解决办法
4948
查看次数