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
  • 相关阅读:
    Python学习笔记(10):异常
    SharePoint中RichTextBox的Required验证
    Python学习笔记(6):模块
    Python学习笔记(4):控制流
    Python学习笔记(5):函数
    解决SharePoint中GridView导出Excel按钮的问题
    Python学习笔记(8):面向对象
    如何在SharePoint中创建Custom Master Page
    main cannot be resolved or is not a field
    c# 格式化输出字符串
  • 原文地址:https://www.cnblogs.com/xiaqiuchu/p/11506883.html
Copyright © 2011-2022 走看看