Cast in query builder laravel

拜拜、爱过 提交于 2020-01-13 13:48:11

问题


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


回答1:


Try it with orderByRaw():

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


来源:https://stackoverflow.com/questions/34713490/cast-in-query-builder-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!