lumen判断时间条件是否执行

版权声明: https://blog.csdn.net/gwz1196281550/article/details/84590013
//该代码解析成 sql 为 select * from count_survey where date >= '$start' and date <= '$end' order by date desc limit 0,20
//该sql要做的是根据日期搜索数据后并分页
DB::table('count_survey')
    //解释该语法  when:何时,何时执行代码。 方法参数:when(需要判断的数据,判断为真的时候执行,判断为假的时候执行)
    ->when(!empty($start), function ($query) use ($start){
        return $query->where('date','>=',$start);
    })
    ->when(!empty($end), function ($query) use ($end){
        return $query->where('date','<=',$end);
    })
     ->orderBy('date','DESC')
     ->offset(($page-1)*$size)
     ->limit($size)
     ->get()
     ->toArray();

猜你喜欢

转载自blog.csdn.net/gwz1196281550/article/details/84590013
今日推荐