【问题标题】:How to send email to all user with condition using php laravel?如何使用 php laravel 向所有有条件的用户发送电子邮件?
【发布时间】:2016-12-14 07:01:27
【问题描述】:

我正在使用 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] => Array ([0] ] => 密码 [1] => remember_token ) [connection:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected ] => Array ( [email] => hed0@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] => [存在] => 1) [1] => App\User Object ([table:protected] => users [hidden:protected] => Array ([0] => password [1] => remember_token) [connection: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 ] => 数组 ( ) [可见:受保护] => 数组( ) [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'),结果就是

hsaput208@test.co

【问题讨论】:

  • 首先获取所有电子邮件,然后在foreach循环中发送电子邮件。现在您在 foreach 循环中收到来自查询的电子邮件,然后您将邮件发送到第一个。也应该是$penerima = User::select('email')->where(...

标签: php laravel email foreach laravel-5


【解决方案1】:

当您执行$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 版本,你应该在你的 pluck 结果中使用toArray...

【讨论】:

  • 我已经像你一样更改了代码但不起作用,它仍然只将电子邮件发送到一个电子邮件。
  • 更新您的问题以反映您的更改
  • 我已经更新了我的问题。顺便说一句,如果我使用 pluck,查询的结果只有一封电子邮件,但如果我使用 get 结果是 3 封电子邮件。当我更改为 get 时,出现错误“Illegal offset type”
  • 你使用的是什么版本的 Laravel?
  • 您能否在您的问题中添加dd($penerima); 的结果?
猜你喜欢
  • 2014-11-23
  • 1970-01-01
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 2017-03-20
相关资源
最近更新 更多