Laj*_*rek 4 laravel eloquent laravel-4
如何在"雄辩的ORM"中获得"自然秩序"?在表I中,我有列'text'(字符串).
正常顺序:Model :: orderBy('text')
'value 1'
'value 12'
'value 23'
'value 3'
'value 8'
Run Code Online (Sandbox Code Playgroud)
我需要这个:
'value 1'
'value 3'
'value 8'
'value 12'
'value 23'
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
您可以添加原始查询并执行以下操作:
Model::orderBy(DB::raw('LENGTH(text), text'));
Run Code Online (Sandbox Code Playgroud)
对于 Laravel 这也适用:
$collection = $collection->sortBy('order', SORT_NATURAL, true);
Run Code Online (Sandbox Code Playgroud)