Reputation: 131
I would like to sort column like orderBy.
'WC' columns has such data
'AB-1' 'AB-2' 'AB-5' 'AB-300' 'AB-1980' .... etc
Front of "AB-" letter is same. then comes number.
Could you teach me how to write code please?
public function index()
{
$images = ImageGallery::orderBy('wc', 'asc')->get();
return view('image-gallery',compact('images'));
}
Upvotes: 0
Views: 54
Reputation: 13394
If you just order by the field, the result will be:
'AB-1' 'AB-1980' 'AB-2' 'AB-200' 'AB-3' 'AB-301'...
You can use order by the length of value, and then order by that field:
ImageGallery::orderBy(DB::raw('LENGTH(wc), wc'))->get();
The result will be :
'AB-1' 'AB-2' 'AB-3' 'AB-200' 'AB-301' 'AB-1980' ...
Upvotes: 1