2016-12-22 20 views
1

我正在使用Laravel項目,並且想要在不喜歡的子句中發佈多個值。我嘗試了以下方式,但沒有成功。無法在不喜歡的子句中傳遞多個值Laravel

$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.*']); 

我也試過把它作爲字符串傳遞,但仍然沒有成功。

回答

2

你可以做到這樣,

$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.*']);  
0

不能在由撇號包裹的字符串中使用數組。 where函數的第三個參數中已經有'%'字符。你爲什麼再次使用你的琴絃?

試試這個:

$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.*']); 
0

爲什麼不使用自定義查詢!

$exclude_emails=['%odz%', '%test.com%']; 
$exclude_emails = implode('|', $exclude_emails); 
SELECT * FROM `vendor_infos` WHERE `email` NOT REGEXP '{$exclude_emails}' . . . 

簡單,不管是什麼尺寸的$exclude_emails

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();