Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hcastillo's avatar

How change the mysql query in query builder Laravel?

Hi guys, i using this query on mysql and work, when y try to convert to query builder the whereBetween() dont accept the date as a column and that spected. my question is how i can use the incoment date as column to search between start_date and end_date columns.

Mysql command

		SELECT 
		r.id_user, 1 as vacation, r.resume
		FROM register r 
		WHERE  "2016-11-16" BETWEEN r.start_date and r.end_date

My Query builder implementation

  $sub_query = DB::connection('personal_actions')
                        ->table('register')
                        ->select("register.id_user","register.resume")
                        ->whereBetween("'2016-11-16'",["register.star_date","register.end_date"])
                        ->get();
0 likes
7 replies
Sinnbeck's avatar

It goes the other way. But for this you can do

->where('star_date', '>=', '2016-11-16')
->where('end_date', '=<', '2016-11-16')
hcastillo's avatar

@Sinnbeck sure

        $sub_query = DB::connection('personal_actions')
                        ->table('register')
                        ->select("register.user_id","register.resume")
                        ->where('register. star_date', '>=', '2016-11-16')
                        ->where('register. end_date', '<=', '2016-11-16')
					    ->get()
Sinnbeck's avatar

@hcastillo Why is there a space between register. and star_date ? and isnt the column named starT date (start_date) ?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

And any chance they are reversed ?

        $sub_query = DB::connection('personal_actions')
                        ->table('register')
                        ->select("register.user_id","register.resume")
                        ->where('register.star_date', '<=', '2016-11-16')
                        ->where('register.end_date', '>=', '2016-11-16')
					    ->get()

Please or to participate in this conversation.