在laravel如期发送电子邮件

时间:2018-04-28 06:31:37

标签: php laravel

我在下面有这个代码,我按计划删除了用户,

protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            $users = User::onlyTrashed()->where(
                'deleted_at', '<=', now()->subDays(1)->toDateTimeString()
            )->get();
            $users->each->forceDelete();
        })->everyMinute(); // this is for test later changes to Daily
    }

我想做什么

是发送电子邮件并告知他们在删除帐户$users->each->forceDelete();之前已删除了他们的帐户,因此我将设置电子邮件添加到我的功能中:

protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            $users = User::onlyTrashed()->where(
                'deleted_at', '<=', now()->subDays(1)->toDateTimeString()
            )->get();
            $user = $users->each;
            Mail::to($user->email)->send(new UserAutoDeleted($user));
            $users->each->forceDelete();
        })->everyMinute(); // this is for test later changes to Daily
    }

并且它一直给我错误,如:

 Symfony\Component\Debug\Exception\FatalThrowableError  : Type error: Argument 1 passed to App\Mail\UserAutoDeleted::__construct() must be an instance of App\User, instance of Illuminate\Support\HigherOrderCollectionProxy given, called in C:\laragon\www\mynewsite\app\Console\Kernel.php on line 35

  at C:\laragon\www\mynewsite\app\Mail\UserAutoDeleted.php: 21
  17:      * Create a new message instance.
  18:      *
  19:      * @return void
  20:      */
  21:     public function __construct(User $user)
  22:     {
  23:         $this->user = $user;
  24:     }
  25:
  26:     /**

  Exception trace:

  1   App\Mail\UserAutoDeleted::__construct(Object(Illuminate\Support\HigherOrderCollectionProxy))
      C:\laragon\www\mynewsite\app\Console\Kernel.php : 35

  2   App\Console\Kernel::App\Console\{closure}()
      C:\laragon\www\mynewsite\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php : 29

  Please use the argument -v to see more details.

任何想法?

1 个答案:

答案 0 :(得分:0)

解决

我把我的邮件代码放到另一个循环函数中,如:

$users->each(function($user) {
  Mail::to($user->email)->send(new UserAutoDeleted($user));
});

它现在工作得很好。