hendraspt hendraspt - 4 months ago 21
PHP Question

How to send email to all user with condition using php laravel?

I am using php Laravel. 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] => hend0@gmail.com ) [original:protected] => Array ( [email] => hend0@gmail.com ) [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] => hsaput208@test.co ) [original:protected] => Array ( [email] => hsaput208@test.co ) [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] => diae@test.co ) [original:protected] => Array ( [email] => diae@test.co ) [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


hsaput208@test.co

Answer

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')
    ->pluck('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...