jeudi 30 avril 2020

How to know what columns are presents in a Eloquent query before to execute it in Laravel 5.5?

Im using Laravel 5.5 and I have and QueryBuilder object (from the "Illuminate/Database/Eloquent/Builder" class).

I want to set an orderBy sentence into my query, but only if this field is present and exists in the QueryBuilder object (as column in the select section sentence).

For example, there is an User model, with the following fields ['id', 'firtsname', 'lastname', 'username','description'].

This is my object:

Use App\User;
$query = User::query();
if ($request->input('sort') != null) {
   $model_query->orderBy($request->input('sort'), 'ASC');
}
$users = $query->get();

When I execute it, works fine (if I send you consistent data, of course). But if I set a column what does not exists, it sends and exception. So, the question is, how I can get the columns to retrieve from my $query object? To validate it, and if it's presents, execute the ordening code.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2L1Dtkx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire