I am trying to make a query using Eloquent Model in Laravel.
My original query does not work
Query1::where('Course_ID', '=', $request->FLA)
->where('Date', '=', Carbon::today())
I would like the query to include both inside of a single WHERE, akin to:
Query1::where('Course_ID', '=', $request->FLA && 'Date', '=', Carbon::today())
Is this possible?
3
Answers
You can use:
It will create the following SQL query:
But your approach, using two ‘where’s will have the same output
The way you have your query written, using
->where()->where()
will generate the following query:If you want your
WHERE
clause to be "scoped", you can use this syntax:This will generate the query:
Notice the
()
aroundCourse_ID = ? AND Date = ?
This is useful when you want to add conditions that need to be grouped, i.e. if you were adding another
WHERE
that could conflict with the existing one(s).If your Date column is type of date, please try like that