Laravel Eloquent - Select MAX with other columns Laravel Eloquent - Select MAX with other columns laravel laravel

Laravel Eloquent - Select MAX with other columns


I think you should rewrite it like this:

DB::table('users')    ->select(['users.id', 'users.username', DB::raw('MAX(ur.rank) AS rank')])    ->leftJoin('users_ranks AS ur', 'ur.uid', '=', 'users.id')    ->where('users.id', '=', 7)    ->groupBy('users.id')    ->first();

No sense to use User:: if you use table names later and want to fetch not all of the fields ( 'users.id', 'users.username' ).