• laravel 多条件查询



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

           // dd( DB::getQueryLog());
    foreach ($cases as $k => $v) {
    $cases[$k]->photo = $this->upload.$v->photo;
    // if ($v->panorama && empty($v->url)) {
    // $cases[$k]->url = 'http://www.homeeyes.cn/app/3DShow/index.html?type=1&case_id='.$v->id;
    // }
    // $cases[$k]->author = DB::table('admin_users')->where('id',$v->uid)->select('name','avatar')->first();
    // if ($cases[$k]->author) {
    // $cases[$k]->author->avatar = $this->upload.$cases[$k]->author->avatar;
    // }
    }
    return response()->json(['error'=>0,'data'=>$cases]);
  • 相关阅读:
    shell常用的系统变量
    Git的使用--如何将本地项目上传到Github
    vmware + centos 7安装vmtools时提示The path "" is not a valid path to the xxx kernel header
    SQL查询表中的有那些索引
    SQL merge into 表合并
    SqlServer coalesce函数
    SQL 大数据查询如何进行优化?
    为什么GOF的23种设计模式里面没有MVC?
    Javascript闭包
    AngularJS概述&指令
  • 原文地址:https://www.cnblogs.com/vinzen/p/10517246.html
Copyright © 2020-2023  润新知