Nov 3, 2016
0
Level 7
Eloquent Bug between Repository & Testing
Hey guys,
Not sure how to solve this issue at the moment, exhausted google and team members.
The following code produces the correct result when called from a Controller... perfect, no issues whatsoever.
public function categoryMonthlyBookings(Request $request = null, $status = null)
{
$query = Booking::join('categories', 'categories.id', '=', 'bookings.category_id');
if (!is_null($status)) {
$query->where('status', $status);
}
$query = $query->select('date', 'categories.display_name', 'categories.id AS category_id', DB::raw('count(*) as count, SUM(total) as total'))
->orderBy('date')
// ->raw('GROUP BY(MONTH(date))')
->groupBy(DB::raw('MONTH(date)'))
->groupBy('category_id')
->get()->groupBy(function ($booking) {
return Carbon::parse($booking->date)->format('m/Y'); // grouping by months
});
return $query;
}
However when it is run via a test in the command line, it returns with:
) RepositoryTest::it_returns_all_bookings_count_and_sum_by_month_year_and_category
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such function: month (SQL: select "date", "categories"."display_name", "categories"."id" as "category_id", count(*) as count, SUM(total) as total from "bookings" inner join "categories" on "categories"."id" = "bookings"."category_id" where "bookings"."deleted_at" is null group by month(date), "category_id" order by "date" asc)
Caused by
PDOException: SQLSTATE[HY000]: General error: 1 no such function: month
If I swap it out with that commented line using ->raw() it works in the controller & test but does not produce the right results. I'm at a loss on this one, not sure why the ->groupBy(DB::raw('MONTH(date)')) does not work in the testing environment.
All help is appreciated
Please or to participate in this conversation.