cathy123
cathy123

Reputation: 543

Order by upcoming months in laravel 5

I want to order the set of records by month in laravel 5.
The problem is when I use order by 'Month', it just ordered the records by alphabets like

apr, aug, dec, feb, jan, jul, jun, may...

But what I want is something like this:

jan,feb,mar,apr,may,jun,jul,aug

It should order by upcoming months.

Here is my code:

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();
}

Can anyone please help me with this?

Upvotes: 0

Views: 518

Answers (2)

karthik
karthik

Reputation: 56

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();

Upvotes: 0

Vahe Galstyan
Vahe Galstyan

Reputation: 1731

You can use STR_TO_DATE

I think it can help you.

Upvotes: 1

Related Questions