Level 104
Wrap %M in quotation marks
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I have the following code and it keeps giving error, if anyone knows of a possible solution I will be glad.
$query = DB::table('patients');
// $query->select(DB::raw("COUNT(`id`) as count, DATE_FORMAT(`created_at`, %M) as date"));
$query->select(DB::raw("DATE_FORMAT(`created_at`, %M)as date, COUNT(`id`) as count")); // Preferable, so that the database can handle it
$query->orderBy('created_at', 'desc');
$query->groupBy('date');
dd($query->get());
$labels = [];
$rows = [];
foreach ($query as $value) {
$labels = $value->created_at;
$rows = $value->fullName;
}
$data = [
'labels' => $labels,
'rows' => $rows
];
return response()->json(['data' => $data]);
I keep getting the following error:
QueryException in Connection.php line 770:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%M)as date, COUNT(`id`) as count from `patients` group by `date` order by `creat' at line 1 (SQL: select DATE_FORMAT(`created_at`, %M)as date, COUNT(`id`) as count from `patients` group by `date` order by `created_at` desc)
Your help is highly appreciated in this regard, Thanks again. Qasim
Wrap %M in quotation marks
Please or to participate in this conversation.