php Laravel 5 在两列上有许多关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29751859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:33:19  来源:igfitidea点击:

Laravel 5 hasMany relationship on two columns

phplaraveleloquentlaravel-5

提问by CharliePrynn

Is it possible to have a hasMany relationship on two columns?

是否可以在两列上建立 hasMany 关系?

My table has two columns, user_idand 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();

回答by Collin James

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();

回答by topclaudy

Composhipsadds 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.

其中两列必须匹配。

回答by tomstig

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.

我在那里提出了一个解决方案,欢迎任何进一步的想法和贡献。

回答by shamaseen

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