0

How to convert below query into Laravel equivalent query builder

SELECT * FROM `table_name` WHERE '03-05-2017 09:30' BETWEEN `start_date` AND `end_date` AND `who_should`='VV000'

I tried using whereBetween but not working as expected.

3
  • Generally column_name BETWEEN '03-05-2017 09:30' AND '10-05-2017 09:30' is the format I am unable to understand the Logic where you are checking the constant value... Commented May 1, 2017 at 7:26
  • There is start_date & end_date columns Commented May 1, 2017 at 7:43
  • You can use where > and < or you can add raw where clause in whereRaw also. Commented May 1, 2017 at 9:10

2 Answers 2

2
$given_time = "03-05-2017 09:30";    
\DB::table('table_name')
        ->whereRaw(" '$given_time' Between start_date  and end_date ")
        ->where("who_should", "=", "VV000")
        ->get();
Sign up to request clarification or add additional context in comments.

Comments

1
$date = new Carbon\Carbon('03-05-2017 09:30'); 
        $date_string=$date->toDateTimeString();

        $data_set = DB::table('table_name')
        ->select(DB::raw('*'))
        ->where('start_date', '<', $date_string )
        ->where('end_date', '>', $date_string )     
        ->where('who_should','=','VV000')
        ->get();

Try above code

9 Comments

I dont know why its showing error syntax error, unexpected '$data_set' (T_VARIABLE)
missed semicolon $date_string=$date->toDateTimeString();
Still getting this syntax error, unexpected '/', expecting ',' or ';'
Its same, no difference
I have changed date_string to $date_string previously, now there should not contain an error, You may also need to call use Carbon; before defining your controller class
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.