Laravel等效查询构建器

时间:2017-05-01 07:09:10

标签: php laravel

如何将以下查询转换为Laravel等效查询构建器

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

我尝试使用 whereBetween 但未按预期工作。

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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();

尝试以上代码