问题描述: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

Laravel where条件拼接,数组拼接where条件的更多相关文章

  1. js数组去重 数组拼接 替换数组中的指定值 递归数组 判断数组中是否存在指定值 数组求和 根据条件判数组值

    这是学习过程中记录的一些关于数组操作的常用属性或方法,记录一下方便以后使用. // 数组去重 var arr1 = [1,1,2,3,4,5,6,3,2,4,5,'a','b','c','a',6,7 ...

  2. php部分--例子:租房子(复选框的全选、数组拼接成字符串、设置复选框的name值、)

    1.链接数据库 <?php include("DBDA.class.php"); $db=new DBDA(); $sql="select * from fangz ...

  3. Java基础知识强化44:StringBuffer类之把数组拼接成指定格式的字符串的案例

    1. 先看案例代码如下: package cn.itcast_07; /* * 把数组拼接成一个字符串 */ public class StringBufferTest2 { public stati ...

  4. PHP 数组拼接成字符串

    PHP[知识分享] 数组拼接成字符串 <?php // 格式: [二维数组] Array ( [0] => Array ( [topicid] => 1 ) [1] => Ar ...

  5. python numpy 数组拼接

    我就写一下我遇到的,更多具体的请看Python之Numpy数组拼接,组合,连接 >>> aarray([0, 1, 2],       [3, 4, 5],       [6, 7, ...

  6. numpy库数组拼接np.concatenate的用法

    concatenate功能:数组拼接 函数定义:numpy.concatenate((a1, a2, ...), axis=0, out=None)

  7. 当update的查询条件是数组的时候,upsert会失效

    不管是findOneAndUpdate还是update方法,只要他们的查询条件是数组,upsert就会失效,比如: //这段代码只会更新已存在的数据,不存在的不会插入 tagModel.update( ...

  8. numpy数组 拼接

    转载自:https://blog.csdn.net/zyl1042635242/article/details/43162031 数组拼接方法一 首先将数组转成列表,然后利用列表的拼接函数append ...

  9. js 数组 添加或删除 元素 splice 创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素 filter

    里面可以用 箭头函数 splice         删除 增加 数组 中元素 操作数组 filter 创建新数组  检查指定数组中符合条件的所有元素

  10. 【Matlab开发】matlab删除数组中符合条件的元素与散点图绘制

    [Matlab开发]matlab删除数组中符合条件的元素与散点图绘制 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ matlab删除数组中符合条件的元素 如 ...

随机推荐

  1. other备忘

    wps CONCATENATE 只是因为格式 设置成了文本,把这列 格式 设置成 常规,双击下 结果就出来了 https://zhidao.baidu.com/question/21208668961 ...

  2. kotlin class

    每个次构造函数需要委托给主构造函数, 可以直接委托或者通过别的次构造函数间接委托.委托到同一个类的另一个构造函数用 this 关键字即可 class Person { constructor(pare ...

  3. SpringCloud(四)之Netflix开源组件断路器Hystrix介绍

    一.前言? 1.Netflix Hystrix断路器是什么? Netflix Hystrix是SOA/微服务架构中提供服务隔离.熔断.降级机制的工具/框架.Netflix Hystrix是断路器的一种 ...

  4. LC 926. Flip String to Monotone Increasing

    A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...

  5. Linux shell利用sed如何批量更改文件名详解[转载]

     需求背景: 目录A用来存放自动化的包生成的apk文件,现在开发修改了包名的 命名规则:环境名称(pro|uat)-release-日期-v版本号.apk 原来的是思路是通过正则表达式匹配新的包名,但 ...

  6. AngularJS ng-disabled在a内无效

    在AngularJS中,对a添加ng-disabled,在disabled情况下,虽显示了不可用的样式,但点击了依旧能触发绑定在a上的ng-click事件. 解决方式:将a改为button.

  7. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用

    笔记 3.热部署在Eclipse和IDE里面的使用     简介:讲解热部署的好处及使用注意事项,在eclipse里面默认开启,在IDE里面默认关闭                  1.增加依赖 & ...

  8. monkey 查找闪退页面的方法

    使用了命令 adb shell monkey  --pct-touch 100 -v -p  com.iBer.iBerAppV2  5000 >/Users/kaibinliu/Desktop ...

  9. What happens when you type an URL in the browser and press enter?

    What happens when you type an URL in the browser and press enter? 1. You type maps.google.com into t ...

  10. socket --自己简单的理解

    一,网络编程中两个主要的问题 一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输. 在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可 ...