Reputation: 969
I am using PHP Laravel v5.0. And I want to send email to all user where divisi = X. The query of $penerima shows 3 emails which divisi = X. But the email just sent to 1 email of 3 emails. Do you know which wrong of my code? thanks
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!');
});
}
}
If I am showing the result of $penerima, the result is
Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [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 ( ) [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 ) [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] => [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 ) [2] => 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 ( ) [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 ) ) )
if I changed the get() into pluck('email'), the result is just
Upvotes: 2
Views: 3670
Reputation: 3315
When you do $penerima[0]
, you just take the first User. You should give to the to
method an array of all the emails, like so
$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
will give you an array of just the given column.
Depending on your Laravel version, you should use toArray
on your pluck result...
Upvotes: 4