如何使用php laravel向有条件的所有用户发送电子邮件?

时间:2016-08-08 13:18:58

标签: php laravel email foreach laravel-5

我使用的是PHP Laravel v5.0。我想向所有divisi = X的用户发送电子邮件。$ penerima的查询显示3封电子邮件,其中divisi = X.但是电子邮件只发送到3封电子邮件中的1封电子邮件。你知道我的代码有哪些错误吗?感谢

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的结果,结果是

  

Illuminate \ Database \ Eloquent \ Collection Object([items:protected] => Array([0] => App \ User Object([table:protected] => users [hidden:protected] =>数组([0] =>密码[1] => remember_token)[连接:受保护] => [primaryKey:protected] => id [perPage:protected] => 15 [递增] => 1 [timestamps] => 1 [attributes:protected] =>数组([email] => hend0@gmail.com)[原创:受保护] =>数组([email] => hend0@gmail.com )[relations:protected] => Array()[visible:protected] => Array()[appends:protected] => Array()[fillable:protected] => Array()[guarded:protected] =>数组([0] => *)[日期:受保护] =>数组()[演员:受保护] =>数组()[触摸:受保护] =>数组()[可观察到:受保护] => Array()[with:protected] => Array()[morphClass:protected] => [exists] => 1)[1] => App \ User Object([table:protected] => users [hidden:protected] =>数组([0] =>密码[1] => remember_token)[connection:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [递增] => 1 [时间戳] => 1 [attributes:protected] =>数组([email] => hsaput208@test.co)[original:protected] =>数组([email] => hsaput208@test.co)[关系:受保护] => Array()[visible:protected] => Array()[appends:protected] => Array()[fillable:protected] => Array()[guarded:protected] =>数组([0] => *)[日期:受保护] => 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 [递增] => 1 [时间戳] => 1 [属性:受保护] =>数组([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] =>数组([0] => *)[日期:受保护] =>数组()[演员:保护] => Array()[touches:protected] => Array()[observables:protected] => Array()[with:protected] => Array()[morphClass:protected] => [存在] => 1)))

如果我将get()更改为pluck('email'),结果只是

  

hsaput208@test.co

1 个答案:

答案 0 :(得分: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 ...