i want to get all user data and sort by ascending order then select required columns
$drivers = Driver::all()
->select('id','first_name','last_name','phone_number','registration_id')
->get();
now i'm getting all the data
thank you
In this case, remove all()
and add an orderBy()
:
$drivers = Driver::select('id','first_name','last_name','phone_number','registration_id')
->orderBy('the-order-column', 'asc or desc')
->get();
The methods all()
and get()
do the same thing, except from that you can't modify the query using all()
(like adding orderBy()
).
Laravels documentation on orderBy()
: https://laravel.com/docs/5.3/queries#ordering-grouping-limit-and-offset
To sort results, just use OrderBy.
For example, if you want to sort by first_name, use :
$drivers = Driver::select('id','first_name','last_name','phone_number','registration_id')
->orderBy('first_name', 'asc')
->get();
change 'asc' with 'desc' if you want descending order.
And don't use All()
if you don't want everything.