How can i get 'natural order' in 'Eloquent ORM'? In table I have column 'text' (string).
Normal order: Model::orderBy('text')
'value 1'
'value 12'
'value 23'
'value 3'
'value 8'
I need this:
'value 1'
'value 3'
'value 8'
'value 12'
'value 23'
Any ideas?
You could add a raw query and do something like this:
Model::orderBy(DB::raw('LENGTH(text), text'));
Or, in modern versions of Laravel:
Model::orderByRaw('LENGTH(text), text');
For Laravel this also works:
$collection = $collection->sortBy('order', SORT_NATURAL, true);
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