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 a skilled programmer specializing in Vue.js/Nuxt.js for front-end development and PHP Laravel for back-end solutions. I have a strong focus on API design and development, complemented by experience in web server setup and maintenance. My versatile expertise ensures seamless creation and maintenance of web applications, covering everything from intuitive user interfaces to robust server-side functionality. Passionate about coding and driven by a lifelong learning mindset, I invite you to explore more at danyal.dk.