skip to Main Content

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


  1. 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 use whereRaw() with the following SQL expression:

    whereRaw('column_name BETWEEN DATE(?) AND DATE(?)', [$dateA, $dateB])
    

    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.

    Login or Signup to reply.
  2. You can do it using whereBetween in the following way:

    $startDate = '2023-06-01';
    $endDate = '2023-07-01';
    
    $users = DB::table('users')
                ->whereBetween('created_at', [$startDate, $endDate])
                ->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search