2014-10-09 99 views
0

我需要從我的數據庫中獲取所有用戶,不包括屬於AdminStaff組的用戶,我需要爲他們每個人獲取他們的個人資料,所以我使用Eager裝載:Laravel - 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); 

我的數據庫表:

users: 
id | email | password 

profile: 
user_id | first_name | last_name 

roles: 
id | name 

role_user: 
user_id | role_id | 

的問題是,上面的代碼與user的相關信息,但只顯示第一profile所以我也有同樣first_name and last_name與不同耳鼻喉科email

+0

您從所有表中選擇多個'id'領域,所以'您的模型id'被覆蓋。使用關係,會更容易。 – 2014-10-09 15:05:23

回答

0

這樣的事情應該工作:

$users = \User::with('profile', 'roles') 
->whereRaw('roles.name', '!=' 'Admin', 'OR', 'roles.name', '!=', 'Staff') 
->withTrashed()->paginate(10);