I am working on a CRM where I have a User model which can have many to many relationships with other User's and a type of relationship which is defined through the pivot table.
For example:
Tim -> Tom ( co-worker )
Tim -> Steve ( family member )
Tim -> Jon ( neighbors )
In my User model I have this where I'm using user1_id and user2_id from the pivot table as foreign keys:
public function relationships ()
{
return $this->belongsToMany(User::class,null,'user1_id','user2_id')->withTimestamps()->withPivot('type');
}
Which works fine in one direction. So if I call this:
User::find($timsID)->relationships;
That works as expected and I'll get Tom Steve and Jon as results. Now, if I were to use Tom's ID in that query instead, Tim won't return as a relationship since it's searching on user1_id when Tom's ID is stored in user2.
Is there a solution to pull the records if there's a match from either foreign key? I don't want to have to store 2 records for each relationship.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2l1xTzY
via IFTTT
Aucun commentaire:
Enregistrer un commentaire