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)
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)
Query Builder
DB::table('table_name')->orderByraw('CHAR_LENGTH(col_name) ASC')->get();
Eloquent ORM
User::orderByRaw('CHAR_LENGTH(col_name)')->get();
In my query, I had to add quotation marks for my field.
User::orderByRaw('CHAR_LENGTH("name")')->get();