0

I wish to get all roles with users, but I want to specify the user.

It's a many to many relationship.

Role::with('users')->get();

The above gets all roles with their users, I wish to get all roles and only display a specific user next to the ones they exist in. I still want roles in which they don't exist in.

I think I need to use a nested query but cant quite get the syntax correct using 'with'.

1 Answer 1

1

You can filter the users inside the "with", like this:

Role::with(array('users' => function($query) use ($username) {
    $query->where('user_name', '=', $username);
}));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.