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 working as programmer since 2002. Born, grew up, studied and worked in Pakistan now Im located in Denmark since 2006.