lundi 2 septembre 2019

Laravel eloquent/collection select only matching records for all requested users

I have a polymorphic "Relationships" many to many relation that looks like that:

users
id | first_name | last_name

groups
id | group_name

regions
id | region_name

relationships
id | user_id | relationship_id | relationship_type | relationship_level
 4 |      13 |              25 |         App\Group |      Group Manager
 5 |      20 |              18 |        App\Region |        Participant

And the relationship looks like that:

public function regions()
{
    return $this->morphedByMany('App\Region', 'relationship')->withPivot('relationship_level')->withTimestamps();
}

public function groups()
{
    return $this->morphedByMany('App\Group', 'relationship')->withPivot('relationship_level')->withTimestamps();
}

And vice versa.

When I have an array of IDs, I want to select the relationships that exactly match between the users, this means that all columns (except user_id) match.

And they should only be rows that match among all users. If only 10 out of 12 selected users have the same relationship it should not be included.

If I select 10 users I need to select a pivot record that all those 10 users have (if any).

It doesn't matter if done in eloquent or via the collection after fetching from Database.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire