我想在laravel 5中按月订购一组记录。
问题是当我使用order by 'Month'
时,它只是按照
apr, aug, dec, feb, jan, jul, jun, may..
。
但我想要的是这样的:
jan,feb,mar,apr,may,jun,jul,aug
应该在即将到来的几个月内订购。
这是我的代码:
public static function getMonths($customer_id = false)
{
$customer = DB::table('customer');
$customer->join('customer_month_calender','customer_month_calender.customer_id','=','customer.id','left');
$customer->select('customer.id','customer.name','customer_month_calender.month');
$customer->orderBy(DB::raw('MONTHNAME(customer_month_calender.month)'), 'asc');
$customer->groupBy('customer.id');
return $customer->get();
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
您可以使用 STR_TO_DATE
我认为它可以帮助你。
答案 1 :(得分:0)
TRY this it will work
public static function getMonths($customer_id = false)
{
$customer = DB::table('customer');
$customer->join('customer_month_calender','customer_month_calender.customer_id','=','customer.id','left');
$customer->select('customer.id','customer.name','customer_month_calender.month');
$customer->orderBy(DB::raw('MONTH(customer_month_calender.month)'), 'asc');
$customer->groupBy('customer.id');
return $customer->get();