其他分享
首页 > 其他分享> > Laravel-多条件检索方案

Laravel-多条件检索方案

作者:互联网

控制器

 

public function index(Request $request)
    {
        $status = $request->input('status');
        $title = $request->input('title');
        $cate_id = $request->input('cate_id');
        $where = [];
        if(!empty($status)){
            $where['status'] = $status;
        }
        if(!empty($cate_id)){
            $where['cate_id'] = $cate_id;
        }
        if(!empty($title)){
            $where['title'] = $title;
        }
        $data = ArticleModel::page($where);
        return view('article.index',compact('data'));
    }

模型层

public static function page($where){
        $model = new self();
        if(isset($where['title'])){
            $model = $model->where('title','like','%'.$where['title'].'%');
        }
        if(isset($where['cate_id'])){
            $model = $model->where('cate_id',$where['cate_id']);;
        }
        if(isset($where['status'])){
            $model = $model->where('status',$where['status']);;
        }
        $data = $model->orderBy('id','desc')
            ->paginate(self::SIZE);
        return $data;

        //onlyTrashed 只要被软删除的数据
        //withTrashed 包含回收站的数据
        //不包括回收站的内容
    }

 

标签:Laravel,检索,cate,方案,title,status,model,where,id
来源: https://www.cnblogs.com/superzwb/p/14981657.html