Home > Enterprise >  Laravel many to many and sub many to many
Laravel many to many and sub many to many

Time:06-26

i have Category model which has many to many relation with user while user model has many to many relation with area and area has one to many relation with city i get data of categories with user by

$categories = Category::where('id',$id)->with('users')->get();

but i don't know how to get users with area and city

CodePudding user response:

just chain the eager loading

$categories = Category::where('id',$id)->with('users.areas.city')->get();

if you only need the user list, better start with that model

$users = User::with('areas.city')
    ->whereHas('categories', function($query) use ($id) {
        $query->where('id', $id);
    })
    ->get();
  • Related