2016-08-08 51 views
2

我使用PHP Laravel v5.0。我想發送電子郵件給divisi = X的所有用戶。$ penerima的查詢顯示了3封電子郵件,其中divisi = X.但是電子郵件剛剛發送到1封電子郵件中的3封電子郵件。你知道我的代碼有哪個錯誤嗎?感謝如何發送電子郵件給所有使用php laravel的條件的用戶?

if ($approve != null){ 
     foreach ($approve as $X) { 
      $penerima = User::select('email') 
          ->where('divisi','=', $X) 
          ->where('deleted','=', '0') 
          ->get(); 

      Mail::send('mail', $data_nomor, function ($m) use ($penerima) { 
       $m->to($penerima)->subject('Respond Reminder!'); 
      }); 
     } 
    } 

如果我顯示的$ penerima結果,結果是

照亮\數據庫\雄辯\集合對象([項目:保護] =>陣列([0] => App \ User Object([table:protected] => users [hidden:protected] => Array([0] => password [1] => remember_token)[connection:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array([email] => [email protected])[original:protected] => Array([ email] => [email protected])[relations:protected] => Array()[visible:protected] => Array()[appends:protected] => Array() Array()[guarded:protected] => Array([0] => *)[dates:protected] => Array()[casts:protected] => Array()[touches:protected ] => Array()[observables:protected] => Array()[with:protected] => Array()[morphClass:protected] => [exists] => 1)[1] => App \ User Object [table:protected] => users [hidden:protected] => Array([0] => password [1] => remember_token)[connection:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array([email] => [email protected])[original:protected] => Array([email] => hsaput208 Array()[appends:protected] => Array()[fillable:protected] => Array()[guarded:protected] = Array()[touches:protected] => Array()[observables:protected] => Array([] => *)[日期:保護] => Array()[casts: )[with:protected] => Array()[morphClass:保存] => [exists] => 1)[2] => App \ User Object([table:protected] => users [hidden:protected] => Array([0] => password [1] => remember_token )[連接:保護] => [primaryKey:保護] =>身份證[每頁:保護] => 15 [遞增] => 1 [時間戳] => 1 [屬性:保護] =>陣列([電子郵件] =>數組([email:] = [email protected])[relations:protected] => Array()[visible:protected] => Array()[appends:protected ] => Array()[fillable:protected] => Array()[guarded:protected] => Array([0] => *)[dates:protected] => Array()[casts:protected] => Array ()[touches:protected] => Array()[observables:protected] => Array()[with:protected] => Array()[morphClass:protected] => [exists] => 1)))

如果我將get()更改爲pluck('email'),結果就是

[email protected]

+0

首先獲得所有電子郵件,然後在'foreach'循環發送電子郵件。現在,您在foreach循環中從查詢中獲取電子郵件,然後將郵件發送到第一個郵件。它也應該是'$ penerima = User :: select('email') - >其中(...' –

回答

2

當你做$penerima[0],你只需要第一個用戶。你應該給到to方法中的所有電子郵件的陣列,像這樣

$emails = User::select('email') 
    ->where('divisi','=', $X) 
    ->where('deleted','=', '0') 
    ->lists('email'); 

Mail::send('mail', $data_nomor, function ($m) use ($emails) { 
    $m->to($emails)->subject('Respond Reminder!'); 
}); 

pluck會給你只是給定列的一個陣列。

根據您的Laravel版本,你應該用你的勇氣結果toArray ......

+0

我已經改變了你的代碼,但不起作用,它仍然只發送電子郵件到一個電子郵件。 – hendraspt

+1

更新您的問題以反映您的更改 – PeterPan666

+0

我已更新我的問題。順便說一句,如果我使用pluck,查詢的結果只有一個電子郵件,但如果我使用得到的結果是3封電子郵件。當我更改爲get時,出現錯誤「非法偏移類型」 – hendraspt

相關問題