How to order by character and number in laravel?

Member

by deron , in category: PHP Frameworks , 3 months ago

How to order by character and number in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , 3 months ago

@deron 

To order by character and number in Laravel, you can use the orderByRaw method in your query builder. Here's an example:

1
2
3
$items = DB::table('items')
    ->orderByRaw('LENGTH(name), name')
    ->get();


In this example, we are sorting the items by the length of the name first, and then by the name itself. This will first sort the items by the number of characters in the name, and then alphabetically.


You can adjust the orderByRaw method according to your specific requirements.