在这种情况下,Laravel是多对多关系


laravel many to many relationship in this case

我已经检查了这个官方示例http://laravel.com/docs/eloquent#many-to-many-polymorphic-relations

但我仍然困惑,因为我可能有不同的情况。

我有一个DetailsAttribute模型,它处理details_attribute表。

我有一个Action模型巫婆处理action表。

他们之间的关系是多对多的。

所以我创建了一个新的表details_attribute_action模型DetailsAttributeAction

我的DetailsAttribute模型应该有:

public function actions(){}

我的Actions模型应该有:

public function detailsAttributes(){}

和我的DetailsAttributeAction模型应该有功能,但我不知道它们是什么。

我的问题是前面的函数里面的代码是什么?DetailsAttributeAction真的应该有not的功能吗?

您正在寻找的是多对多关系,而不是多态关系。
http://laravel.com/docs/eloquent多对多

你的代码应该看起来像这样:

class DetailsAttribute extends Eloquent {
    // ...
    public function actions()
    {
        // Replace action_id and details_attribute_id with the proper
        // column names in the details_attribute_action table
        return $this->belongsToMany('Action', 'details_attribute_action', 'details_attribute_id', 'action_id');
    }
}
class Action extends Eloquent {
    // ...
    public function detailsAttributes()
    {
        return $this->belongsToMany('DetailsAttribute', 'details_attribute_action', 'action_id', 'details_attribute_id');
    }
}

你不必担心如何在Laravel中创建DetailsAttributeAction模型。它只是一个表,用于映射您创建的多对多关系。