简体   繁体   English

Laravel 5 在两列上有许多关系

[英]Laravel 5 hasMany relationship on two columns

Is it possible to have a hasMany relationship on two columns?是否可以在两列上建立 hasMany 关系?

My table has two columns, user_id and related_user_id .我的表有两列, user_idrelated_user_id

I want my relation to match either of the columns.我希望我的关系匹配任一列。

In my model I have在我的模型中,我有

public function userRelations()
{
    return $this->hasMany('App\UserRelation');
}

Which runs the query: select * from user_relations where user_relations.user_id in ('17', '18') .运行查询: select * from user_relations where user_relations.user_id in ('17', '18')

The query I need to run is:我需要运行的查询是:

select * from user_relations where user_relations.user_id = 17 OR user_relations.related_user_id = 17 

EDIT:编辑:

I'm using eager loading and I think this will affect how it will have to work.我正在使用预先加载,我认为这会影响它的工作方式。

$cause = Cause::with('donations.user.userRelations')->where('active', '=', 1)->first();

I don't think it's possible to do exactly what you are asking.我认为不可能完全按照你的要求去做。

I think you should treat them as separate relationships and then create a new method on the model to retrieve a collection of both.我认为您应该将它们视为单独的关系,然后在模型上创建一个新方法来检索两者的集合。

public function userRelations() {
    return $this->hasMany('App\UserRelation');
}

public function relatedUserRelations() {
    return $this->hasMany('App\UserRelation', 'related_user_id');
}

public function allUserRelations() {
    return $this->userRelations->merge($this->relatedUserRelations);
}

This way you still get the benefit of eager loading and relationship caching on the model.通过这种方式,您仍然可以在模型上获得预先加载和关系缓存的好处。

$cause = Cause::with('donations.user.userRelations', 
        'donations.user.relatedUserRelations')
    ->where('active', 1)->first();

$userRelations = $cause->donations[0]->user->allUserRelations();

Compoships adds support for multi-columns relationships in Laravel 5's Eloquent. Compoships在 Laravel 5 的Eloquent 中添加了对多列关系的支持。

It allows you to specify relationships using the following syntax:它允许您使用以下语法指定关系:

public function b()
{
    return $this->hasMany('B', ['key1', 'key2'], ['key1', 'key2']);
}

where both columns have to match.其中两列必须匹配。

I'd prefer doing it this way:我更喜欢这样做:

public function userRelations()
{
    return UserRelation::where(function($q) {
        /**
         * @var Builder $q
         */
        $q->where('user_id',$this->id)
            ->orWhere('related_user_id',$this->id);
    });
}

public function getUserRelationsAttribute()
{
    return $this->userRelations()->get();
}

If anyone landed here like me due to google: As neither merge() (as suggested above) nor push() (as suggested here ) allow eager loading (and other nice relation features), the discussion is still ongoing and was continued in a more recent thread, see here: Laravel Eloquent Inner Join on Self Referencing Table如果有人在这里登陆我一样,由于谷歌:由于没有合并()(以上所建议的),也不推()(如建议在这里)允许预先加载(和其他不错的关系特征),讨论仍在进行中,并继续在最近的线程,请参见此处: Laravel Eloquent Inner Join on Self Referencing Table

I proposed a solution there , any further ideas and contributions welcome.我在那里提出了一个解决方案,欢迎任何进一步的想法和贡献。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM