mardi 5 avril 2016

Laravel: change a raw query in a "query-builder" or "eloquent" one

I have this Laravel Query Builder snippet that is working fine:

$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);

Is there a way to avoid use of raw queries and get the same result? In other words, how can I write this in a more "query-builder" style? Can I also translate this into an Eloquent query?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire