mardi 15 mars 2016

How to order by a specific value in laravel?

I need allow a user to order by a specific value of a column in laravel. In SQL I can achieve this with order by column = 5 but the default laravel orderBy function does not seem to support this.

I found the orderByRaw function and can do it through that but since I'm excepting the value as input from the user, I'm worried about the possibility of SQL injection.

The value is the ID of a related table so I could run a find on the model and the retrieve the ID from the returned model so that the user's input does not get added directly to the query. e.g.

$role = Role::find($userInputValue);
$query->orderByRaw('role_id = ' . $role->id . ' desc');

Is there any easier way to achieve this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire