Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 Laravel:如何使用eloquent搜索数据库中给定的字符串?_Php_Mysql_Laravel_Laravel 4_Eloquent - Fatal编程技术网

Php Laravel:如何使用eloquent搜索数据库中给定的字符串?

Php Laravel:如何使用eloquent搜索数据库中给定的字符串?,php,mysql,laravel,laravel-4,eloquent,Php,Mysql,Laravel,Laravel 4,Eloquent,我想检索与搜索字符串匹配的数据,但前提是它不能属于当前用户 if($str_search && $str_search != '' ) { $mandate_list = Mandate::select(array('umrn','mandate_status', 'payment_type','currency','fixed_amount','max_amount','user_id','id','debtor_bank','creditor_bank','refusal

我想检索与搜索字符串匹配的数据,但前提是它不能属于当前用户

if($str_search && $str_search != '' )
{
  $mandate_list = Mandate::select(array('umrn','mandate_status', 'payment_type','currency','fixed_amount','max_amount','user_id','id','debtor_bank','creditor_bank','refusal_reason'))
                        ->orWhere('id', 'LIKE', '%'.$str_search.'%')
                        ->orWhere('umrn','LIKE','%'.$str_search.'%')
                        ->orWhere('mandate_status','LIKE','%'.$str_search.'%')
                        ->orWhere('mandate_date','LIKE','%'.$str_search.'%')
                        ->orWhere('payment_type','LIKE','%'.$str_search.'%')
                        ->orWhere('currency','LIKE','%'.$str_search.'%')
                        ->orWhere('fixed_amount','LIKE','%'.$str_search.'%')
                        ->orWhere('mandate_date','LIKE','%'.$str_search.'%')   
                        ->where('user_id', '<>', $current_user) 
                        ->orderBy('updated_at', 'desc')                            
                        ->get();

}
if($str\u search&&$str\u search!='')
{
$委托书列表=委托书::选择(数组(“umrn”、“委托书状态”、“付款类型”、“货币”、“固定金额”、“最大金额”、“用户id”、“id”、“债务人银行”、“债权人银行”、“拒绝原因”))
->orWhere('id','LIKE','%.$str_search.'%'))
->或where('umrn','LIKE','%.$str_search.'%'))
->orWhere('ORDUCTION_status'、'LIKE'、'%'.$str_search.'%'))
->orWhere('ORDATE'、'LIKE'、'%'。$str\U search.'%'))
->orWhere('payment_type'、'LIKE'、'%'。$str_search.'%'))
->orWhere('currency'、'LIKE'、'%'。$str_search.'%'))
->orWhere('fixed_amount'、'LIKE'、'%'。$str_search.'%'))
->orWhere('ORDATE'、'LIKE'、'%'。$str\U search.'%'))
->其中('user\u id','',$current\u user)
->orderBy('updated_at','desc')
->get();
}
但此查询显示所有记录(甚至属于当前用户)。
我怎样才能解决这个问题。谢谢你的建议

将所有的
组合在一个闭包中:

requirement::select([…])->where(函数($query)use($stru search)
{
$columns=['id'、'umrn'、'授权状态'、…];
foreach($columns作为$column)
{
$query->orWhere($column,'LIKE','%.$str_search'%');
}
})
->其中('user\u id','',$current\u user)
->orderBy('updated_at','desc')
->get();

当我实现这段代码时,我得到了
{“error”:{“type”:“ErrorException”,“message”:“未定义的变量:str_search”,“file”
foreach
中。您可以使用
use
将$str_search变量传递给函数回调:
[…]->其中(函数($query)use($str search){[…]))
@nithilgorge-更新了
使用
您的
$stru搜索
变量的答案。