laravel_blog_image

Laravel: Raw query for between operator

Example for raw query of between operator. Let’s suppose you have a following records with dates classification, and would like to fetch where current date fits in between start_date and end_date:

+---------------------+-----------
| id | start_date     |  end_date |
+---------------------+-----------+
|   1  | 2020-01-10   | 2020-20-15|
|   2  | 2020-08-01   | 2020-08-31|
|   3  | 2020-09-01   | 2020-09-10|
+------+--------------+-----------+

Query would be as following:

$now = Carbon::now();
$records = DB::table('table_name')
->whereRaw('"'.$now.'" between `start_date` and `end_date`')
->get();
Author: Danyal
I'm skilled programmer with expertise in Vue.js/Nux.js for front-end development and PHP Laravel for back-end development. I excel in building APIs and services, and also have experience in web server setup & maintenance. My versatile skill set allows you to develop and maintain web applications effectively, from the user interface to the server-side functionality. I love coding with never ending learning attitude, thanks for visiting danya.dk