Cast in query builder laravel
Asked Answered
A

1

10

I want to cast orderBy in query builder laravel

because my price is varchar type..

so when it's sorting...the result is far from I want...

my script like this

DB::table('test')->where(...)->orderBy('price')->get();

i already try something like this

$query = CAST(price AS DECIMAL(10,2)) DESC;
DB::table('test')->where(...)->orderBy($query)->get();

how can I cast OrderBy query builder so I can sort price desc

Acrylic answered 11/1, 2016 at 2:51 Comment(0)
O
15

Try it with orderByRaw():

$query = "CAST(price AS DECIMAL(10,2)) DESC";
DB::table('test')->where(...)->orderByRaw($query)->get();
Objectify answered 11/1, 2016 at 3:1 Comment(2)
try $query = "CAST(price AS DECIMAL(10,2)) DESC"; with quotation marks.Goal
Glad that I could help. Cheers.Goal

© 2022 - 2024 — McMap. All rights reserved.