i need to search for records created from one date to another date.
$users = DB::table('users')
->whereDate('created_at', '2023-06-01')
->get();
The query that i make searches me only on a specific date, how can search between two dates?
2023-06-01 at 2023-07-01
2
Answers
If you’re using
whereBetween()
in your query, it will give you data that exists between two dates, excluding the data on date B. However, if you want to include the data created on date B as well, you can usewhereRaw()
with the following SQL expression:This way, you’ll get all the data that falls within the range starting from date A up to and including date B. Remember to replace ‘column_name’ with the actual column you want to filter and
$dateA
and$dateB
with the specific dates you are interested in.Keep in mind that using
whereRaw()
allows you to directly write SQL expressions, so make sure to validate and sanitize the input to prevent any SQL injection vulnerabilities.You can do it using
whereBetween
in the following way: