How to order by character and number in laravel?

Member

by deron , in category: PHP Frameworks , 25 days ago

How to order by character and number in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , 24 days 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.