Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 hasMany relationship on two columns

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

My table has two columns, user_id and related_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').

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();
like image 214
CharliePrynn Avatar asked Apr 20 '15 15:04

CharliePrynn


2 Answers

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();
like image 185
Collin James Avatar answered Nov 02 '22 09:11

Collin James


Compoships adds support for multi-columns relationships in Laravel 5's 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.

like image 20
topclaudy Avatar answered Nov 02 '22 09:11

topclaudy