0

I need to get all the user from my database excluding only the user that belongs to Admin and Staff group and I need, for each of them, to get their profile so I used the Eager Loading:

$data['users'] = User::with('profile')->join('role_user', function($join){
    $join->on('role_user.user_id', '=' , 'users.id');
})
->join('roles', function($join){
    $join->on('roles.id', '=', 'role_user.role_id');
})
->whereNotIn('roles.name', ['Admin', 'Staff'])
->withTrashed()
->paginate(10);

My DB Table:

users:  
id | email | password

profile:
user_id | first_name | last_name

roles:
id | name

role_user:
user_id | role_id |

The problem is that the code above works with user infos but shows only the first profile so I have the same first_name and last_name with different email

Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
  • You are selecting multiple `id` fields from all the tables, so `id` on your models gets overriden. Use relationship, will be easier. – Jarek Tkaczyk Oct 09 '14 at 15:05

1 Answers1

0

something like this should work:

   $users = \User::with('profile', 'roles')
->whereRaw('roles.name', '!=' 'Admin', 'OR', 'roles.name', '!=', 'Staff')
->withTrashed()->paginate(10);
Keith Mifsud
  • 1,599
  • 1
  • 16
  • 26