2016-07-20 16 views
0

Eloquentで次のクエリを実行するにはどうすればよいですか?Laravelの逆関係の検索

SELECT count(*) as total, age 
FROM user 
JOIN role ON user.role_id = role.id 
WHERE role.name = 'maintenance' 
GROUP BY age 

( '年齢' のユーザーにある場合)

答えて

1

未テスト:

$total = App\User::whereHas('roles', function ($query) { 
    $query->where('name', 'maintenance'); 
})->groupBy('age')->count();