Using laravel and I'm creating a select box on a form. I've been using the helper to create the select box and all is working fine.
I retrieve the data for the select box from a database and use the following to retrieve the data:
$data = model::lists('name','id')
Again all works ok and this returns the expected array
My problem though is I can't seem to sort this list - i've tried adding orderBy() but no joy.
Other than using a native php function is there a laravel method to sort a list?
You can put whatever you want, and then list it. I mean:
model::orderBy('orderByColumn')->lists('name', 'id');
As long as lists
is the last method in the chain, other methods works just fine.
Starting from Laravel version 5.3 lists
is going to be deprecated, use pluck
instead:
model::orderBy('orderByColumn')->pluck('name', 'id');
You can try:
$data = model::select('name','id')->orderBy('name');
If that doesn't work, toss a ->get()
on the end:
$data = model::select('name','id')->orderBy('name')->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With