laravel 多条件查询

$cases = DB::table('cases')->where('cid',$cid)->join('admin_users', 'cases.uid', '=', 'admin_users.id')->select('cases.*','admin_users.name','admin_users.avatar')->when($condition,function($query) use($condition){
                return $query->where(function($query) use($condition){
                    $query->orwhere('cases.title','like','%'.$condition.'%')->orwhere('cases.style','like','%'.$condition.'%')->orwhere('cases.address','like','%'.$condition.'%');
                });
            })->when($default,function($query) use($default){
                return $query->where(function($query) use($default){
                    $query->where('cases.style','like','%'.$default.'%');
                });
            })->when($time == 1,function($query){
                return $query->orderBy('cases.addtime','desc');
            })->when($time == 0,function($query){
                return $query->orderBy('cases.addtime','asc');
            })->when($hot == 1,function($query){
                return $query->orderBy('cases.hot','desc');
            })->when($hot == 0,function($query){
                return $query->orderBy('cases.hot','asc');
            })->when($minarea > 0 && $maxarea > 0,function($query) use($minarea,$maxarea){
                return $query->wherebetween('cases.area',[$minarea,$maxarea]);
            })->when($minarea > 0 && $maxarea == 0,function($query) use($minarea){
                return $query->where('cases.area','>=',$minarea);
            })->when($minarea == 0 && $maxarea > 0,function($query) use($maxarea){
                return $query->where(cases.'area','<=',$maxarea);
            })
                ->orderBy('cases.sort','desc')->get();

  

posted @ 2019-05-28 16:01  isafer_wilson  阅读(1691)  评论(0编辑  收藏  举报