zoukankan      html  css  js  c++  java
  • Laravel where条件拼接,数组拼接where条件

    问题描述:laravel where 条件拼接 Like出错,搜索不到要搜索的内容。

    问题代码:

            // 作物
            $crop_class_id = $request->crop_class_id;
            if(!empty($crop_class_id)){
                $where['crop_class_id'] = intval($crop_class_id);
            }
            // 标题
            $title = $request->title;
            if(!empty($title)){
                // 这里的where获取不到like条件
                $where['title'] = ['like', '%'.$title.'%'];
            }

    正确代码:

            // 作物
            $crop_class_id = $request->crop_class_id;
            if(!empty($crop_class_id)){
                $where['crop_class_id'] = intval($crop_class_id);
            }
            // 标题
            $title = $request->title;
            if(!empty($title)){
                // 如果拼接where条件存在比较符号,则不能指定key
                $where[] = ['title', 'like', '%'.$title.'%'];
            }

    整体代码:

    // 获取文章列表
        // @url api/user/article?article_class_id=1&status=1&crop_class_id=1
        public function index(Request $request)
        {
            $where = [];
            // 分类
            $article_class_id  = $request->article_class_id;
            if(!empty($article_class_id)){
                $where['article_class_id'] = intval($article_class_id);
            }
            // 状态
            $status = $request->status;
            if(!empty($status)){
                $where['status'] = intval($status);
            }
            // 作物
            $crop_class_id = $request->crop_class_id;
            if(!empty($crop_class_id)){
                $where['crop_class_id'] = intval($crop_class_id);
            }
            // 标题
            $title = $request->title;
            if(!empty($title)){
                $where[] = ['title', 'like', '%'.$title.'%'];
            }
            $limit  = $request->input('limit');
            $deviceRegionList = ArticleModel::where($where)->orderBy('id','desc')->with('user', 'article_class', 'crop_class')->paginate($limit)->toArray();
            $returnData = [];
            $returnData['msg']              = "查询成功";
            $returnData['count']            = $deviceRegionList['total'];
            $returnData['current_page']     = $deviceRegionList['current_page'];
            $returnData['data']             = $deviceRegionList['data'];
            return success($returnData);
        }

    参考连接:

    1. https://www.cnblogs.com/djwhome/p/9176338.html
  • 相关阅读:
    springmvc
    POJ 3683 Priest John's Busiest Day
    POJ 3678 Katu Puzzle
    HDU 1815 Building roads
    CDOJ UESTC 1220 The Battle of Guandu
    HDU 3715 Go Deeper
    HDU 3622 Bomb Game
    POJ 3207 Ikki's Story IV
    POJ 3648 Wedding
    HDU 1814 Peaceful Commission
  • 原文地址:https://www.cnblogs.com/xiaqiuchu/p/11506883.html
Copyright © 2011-2022 走看看