Michael Anaman
Michael Anaman

Reputation: 199

Syntax Error or access violation - SQL query & Laravel

In my query, i am trying to compare today's date only (without time) to my field created_at but i get the error below

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 '2018-08-29 DATE(created_at)' at line 1

Controller

$item_shipped =  Item::where('id',Auth::user()->id)->whereRaw('DATE(created_at)' , '=', Carbon::now()->toDateString())->get();

        return $item_shipped;

How do i solve this problem ?

Upvotes: 0

Views: 415

Answers (1)

Phiter
Phiter

Reputation: 14992

whereRaw doesn't accept the operator parameter. You have to write the whole condition and it takes the second parameter which is an array of bindings.

You need this:

->whereRaw('DATE(created_at) = ?', [Carbon::now()->toDateString()])->get();

Upvotes: 2

Related Questions