Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-按as编号排序(int),即使列类型为字符串_Php_Laravel_Eloquent_Query Builder - Fatal编程技术网

Php Laravel-按as编号排序(int),即使列类型为字符串

Php Laravel-按as编号排序(int),即使列类型为字符串,php,laravel,eloquent,query-builder,Php,Laravel,Eloquent,Query Builder,这是:- $query = Section::orderBy("section", "desc")->get(); 这里的部分是一个字符串类型的列,但其中有数字,我想按这些数字排序 感谢您的回复首先,您应该在设计模式时使用正确的数据类型 对于现有的模式,可以使用orderByRaw方法调整orderby子句,在运行时键入cast值 ->orderByRaw('section * 1 desc') 你可以用 为什么要有一列存储数字并将其设置

这是:-

$query = Section::orderBy("section", "desc")->get();
这里的部分是一个字符串类型的列,但其中有数字,我想按这些数字排序 感谢您的回复

首先,您应该在设计模式时使用正确的数据类型

对于现有的模式,可以使用
orderByRaw
方法调整orderby子句,在运行时键入cast值

->orderByRaw('section * 1 desc') 
你可以用


为什么要有一列存储数字并将其设置为字符串?只需创建一个迁移,将列更新为整数,然后就可以更轻松地按数字排序。它应该在mysql或pg中工作。您会得到什么结果?虽然这段代码可能会回答这个问题,但如果您解释原因和方式,将对OP和未来的读者有更多帮助
$query = Section::orderByRaw('CONVERT(section, SIGNED) desc')->get();
$query->orderByRaw("section::int", "desc")->get();