Php 无法在not like子句Laravel中传递多个值
我正在从事一个Laravel项目,希望在not like子句中发布多个值。我试过下面的方法,但没有成功Php 无法在not like子句Laravel中传递多个值,php,laravel-5.1,laravel-query-builder,Php,Laravel 5.1,Laravel Query Builder,我正在从事一个Laravel项目,希望在not like子句中发布多个值。我试过下面的方法,但没有成功 $exclude_emails=['odz', 'test.com']; $vendors=\DB::table("vendor_infos") ->where("vendor_infos.email", 'not like', '%'.$exclude_emails.'%' ) ->orderByRaw("
$exclude_emails=['odz', 'test.com'];
$vendors=\DB::table("vendor_infos")
->where("vendor_infos.email", 'not like', '%'.$exclude_emails.'%' )
->orderByRaw("RAND()")->take(8)->get(['vendor_infos.*']);
我也尝试将其作为字符串传递,但仍然没有成功。不能在用撇号包装的字符串中使用数组。where函数的第三个参数中已经有“%”字符。你为什么在你的弦乐中再次使用
$exclude_emails=['odz', 'test.com'];
$vendors=\DB::table("vendor_infos")
->where("vendor_infos.email", 'not like', '%'.$exclude_emails.'%' )
->orderByRaw("RAND()")->take(8)->get(['vendor_infos.*']);
试试这个:
$vendors=\DB::table("vendor_infos")
->where("vendor_infos.email", 'not like', '%odz%')
->where("vendor_infos.email", 'not like', '%test.com%')
->orderByRaw("RAND()")->take(8)->get(['vendor_infos.*']);
你可以这样做
$query = DB::table("vendor_infos");
foreach($exclude_email as $v){
$query->where("vendor_infos.email",'not like','%'.$v.'%');
}
$vendors = $query->orderByRaw("RAND()")->take(8)->get(['vendor_infos.*']);
我希望这能奏效
编辑
或者你可以试试其他方法
$exclude_emails = [
['vendor_infos.email' ,'not like','%'.'odz'.'%'],
['vendor_infos.email' ,'not like','%'.'test.com'.'%'],
];
$vendors=\DB::table("vendor_infos")
->where($exclude_emails)
->orderByRaw("RAND()")->take(8)->get(['vendor_infos.*']);
为什么不使用自定义查询
$exclude_emails=['%odz%', '%test.com%'];
$exclude_emails = implode('|', $exclude_emails);
SELECT * FROM `vendor_infos` WHERE `email` NOT REGEXP '{$exclude_emails}' . . .
无论$exclude\u电子邮件的大小如何,都很简单
Laravel路:
如果您坚持使用laravel,您可以这样做:
// making conditions array
foreach($exclude_emails as $e){
$final_conditions[] = ['email', 'not like', $e];
}
作为@AmitGupta的查询回答:
DB::table("vendor_infos")->where( $final_conditions)->orderByRaw("RAND()")->take(8)->get();