karthick
karthick

Reputation: 1322

fetching data from db based on datetime using laravel query

I am trying to fetch data from database based on datetime in laravel. But the result returns empty array.When I use the raw query from log file in MYSQL,It displays the results. Thanks In Advance

Below is my laravel query

public function lists(){
$start_date = '20-5-2015';
$end_date = '28-5-2015';
$str_start_date = date("Y-m-d H:i:s",strtotime($start_date));
$str_end_date = date("Y-m-d H:i:s",strtotime($end_date));
$result = DB::table('a')
    ->where('start_date_time','>=',"'$str_start_date'")
    ->where('end_date_time','<=',"'$str_end_date'")
    ->select('a.*')
    ->get();
 dd($result);
}

Below is my raw query for the above laravel query

select `a`.* from `a` where `start_date_time` >= '2015-05-20 00:00:00' and `end_date_time` <= '2015-05-28 00:00:00'

Upvotes: 1

Views: 1329

Answers (1)

Marc B
Marc B

Reputation: 360592

->where('start_date_time','>=',"'$str_start_date'")
                                ^---------------^

You're embedding extra quotes inside your values...

Upvotes: 2

Related Questions