I'm trying to group data by month for the current year. The query below works on MySQL database but gives an error when connected to PostgreSQL.
$results = DB::table('visits')
->select(DB::raw('month(visitdate) month, count(*) data'))
->where('team_id', 2)
->whereYear('visitdate', now()->year)
->groupBy('month')
->get();
This error was generated:
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "month" LINE 1: select month(visitdate) month, count() data from "visits" w... ^ (SQL: select month(visitdate) month, count() data from "visits" where "team_id" = 2 and extract(year from "visitdate") = 2021 group by "month")
I tried a similar query with Eloquent and got the same results.
The visitdate column stores the date in the format "2021-01-01"
I'm using laravel 8 with Breeze