0
votes

I've created a table named - 'clients' which has Many to Many Relationship with another table named - 'categories'.

I've 4 tables with their columns below :-

  • 'users' - id, category_id

  • 'categories' - id, name

  • 'categories_users' - id, user_id, category_id (Many to Many)

  • 'requests' - id, category_id

I want to grab those requests which has same categories as of users categories which are saved in the 'categories_client'.

1

1 Answers

0
votes

Requests would need to belongTo() a Category, something like the following should then work.

// Create empty collection
$requests = new Illuminate\Support\Collection();

// Loop through all the categories a user has and merge their requests
//    into our empty collection
$user->categories->each(function($category) use (&$requests) {
    $requests->merge($category->requests);
});

I don't believe there is a built in relation to support this through a pivot table, like there is hasManyThrough()