Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在array laravel 5.2中使用like运算符?_Php_Mysql_Laravel 5.2 - Fatal编程技术网

Php 如何在array laravel 5.2中使用like运算符?

Php 如何在array laravel 5.2中使用like运算符?,php,mysql,laravel-5.2,Php,Mysql,Laravel 5.2,我想在数组中添加Like运算符,但它给了我错误: where子句中的未知列“0”(admin\u id=2和0=name和Like=%zdgbdsh%)按idasc限制10偏移量0)排序 我的问题是:- $conditions = array(); if(!empty($data['name'])) { $conditions = array_merge($conditions,array('name','Like'=>'%'.$data['name'].'%')); } 这是

我想在数组中添加
Like
运算符,但它给了我错误:

where子句中的未知列“0”(
admin\u id
=2和
0
=name和
Like
=%zdgbdsh%)按
id
asc限制10偏移量0)排序

我的问题是:-

$conditions = array();
if(!empty($data['name'])) {
    $conditions = array_merge($conditions,array('name','Like'=>'%'.$data['name'].'%'));
} 
这是我的最后一个问题:-

$querys = DB::table('users')
         ->where($conditions)
         ->skip($iDisplayStart)->take($iDisplayLength)
         ->OrderBy($orderby,$dir)
          ->get();
注意:我不想直接在查询中

where('users.name','like','%'.$data['name'].'%');**
我想在可变条件下做
我使用的是laravel framework 5.2

这并不是应用多个条件的最佳方式,但您始终可以使用whereRaw

所以用你的条件循环一个数组

$conditions = [];
if (!empty($data['name'])) {
   $conditions = array_merge(
       $conditions,
       ["name LIKE '%".$data['name']."%'"]
   );
}

$query = DB::table('users');

// apply add raw conditions
foreach($conditions as $condition){ $query->whereRaw($condition); }

$query->skip($iDisplayStart)->take($iDisplayLength)
      ->OrderBy($orderby,$dir)
      ->get();
你也可以找到一些软件包来帮助你解决类似的问题

希望它有帮助

它应该是:

$conditions=array_merge($conditions,array('name' => 'Like %'.$data['name'].'%'));
更新 我刚刚意识到这将产生
name=Like%data%

您可以将其更改为:

$conditions=array_merge($conditions,array('name', '%'.$data['name'].'%'));
以及在何处使用:

->where(join(' LIKE ', $conditions))

现在没有出现错误,但我的数据没有被提取,它给了我一个空结果:(我已搜索到匹配的记录,但没有数据将被提取列未找到:1054“where子句”中的未知列“2”(SQL:选择
用户
*,
管理员
用户名作为
方法
状态
作为
用户状态
用户
内部连接
管理员
关于
用户
管理id管理员关于
用户的
内部连接
状态
de>
state\u id
=
states
id
其中(
admin\u id
=2)和
2
id
asc limit 10 offset 0)为空,这对我有效$test=\DB::table('users');$test->whereRaw(“类似于“%test%”的电子邮件”);$results=$test->get();尝试将您的查询分解并删除跳过,看看这是否有帮助,以便我们可以定义其分解的位置我已进行了调试,但这一行出现错误[“名称类似“%”“%$data[“名称”]。“%”“]bro我已将未出现问题的分号放在代码中:(我已经执行了这个查询,但这给了我一个空结果$query=User::where(['city'=>'Ludhiana','users.mobile'=>'Like%.'872'.%'))->get();$query=json_decode(json_encode($query),true);echo'';print_r($query);die;$query=User::where('city','Ludhiana')->where('mobile','Like','%872%)->get();