0

In laravel Eloquent: Relationships I want to use two foreign key and two owner key.

How can I do this?

public function user()
{
    return $this->belongsTo(
        User::class,
        'foreign_key1',
        'owner_key1',
        'foreign_key2',
        'owner_key2'
    );
}
linktoahref
  • 7,812
  • 3
  • 29
  • 51

2 Answers2

0

The work around is you can create 2 relationship methods to specify each User's relationship, if this is what you really want:

public function firstUser()
{
    return $this->belongsTo(User::class, 'foreign_key1', 'owner_key1');
}

public function secondUser()
{
    return $this->belongsTo(User::class, 'foreign_key2', 'owner_key2');
}

I don't think there is any other way to solve this.

If this answer is not good for you, consider adding more explanation to the question like what linktoahref said.

felixbmmm
  • 362
  • 3
  • 13
0

I don't think Eloquent supports composite keys, see this post.

In that post they mention this package https://github.com/topclaudy/compoships that provides support for it.

Eugene van der Merwe
  • 4,390
  • 1
  • 35
  • 48