Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel order by field length

I want to build a query that order data by certain field length. I need to rewrite this query to laravel's Eloquent ORM.

SELECT * FROM table ORDER BY CHAR_LENGTH(field)
like image 599
Coze Avatar asked Dec 11 '22 10:12

Coze


1 Answers

It seems that orderByRaw is what you're looking for. Here's an example:

User::orderByRaw('CHAR_LENGTH(name)')->get();

like image 93
kajetons Avatar answered Dec 20 '22 07:12

kajetons