该表有一个 VARCHAR
类型的 startDate 列,因此需要使用该列按升序获取行。
尝试过这个:
orderBy(DB::raw("DATE(startDate)"), 'ASC')
上面没有返回正确的顺序,如何通过clouse按顺序传递字符串日期?
示例:
'startDate' => '2017 年 11 月 7 日'
$items = DB::table("mytable")->orderBy(DB::raw("DATE(startDate)"), 'ASC')->where('userId','=' ,$userId)->get();
最佳答案
您需要将日期转换为 MySQL 格式。尝试使用DATE_FORMAT
:
$items = DB::table("mytable")
->orderBy(DB::raw("DATE_FORMAT(startDate,'%d-%M-%Y')"), 'ASC')
->where('userId','=',$userId)
->get();
关于php - 拉拉维尔 : Order by string date using query builder with MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47940994/