我有这个运行良好的 Laravel 查询生成器片段:
$records = DB::table('users')
->select(
DB::raw('users.*, activations.id AS activation,
(SELECT roles.name FROM roles
INNER JOIN role_users
ON roles.id = role_users.role_id
WHERE users.id = role_users.user_id LIMIT 1)
AS role')
)
->leftJoin('activations', 'users.id', '=', 'activations.user_id')
->where('users.id', '<>', 1)
->orderBy('last_name')
->orderBy('first_name')
->paginate(10);
有没有办法避免使用原始查询并获得相同的结果?换句话说,我怎样才能以更“查询构建器”的风格编写它?我也可以将其翻译成 Eloquent 查询吗?
谢谢