Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

php - sending email on schedule in laravel

I have this code below where i delete users on schedule,

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
    }

what i want to do

is to send email and inform them that their account has been deleted right before i delete their account which is $users->each->forceDelete(); so I added my setup email to my function like:

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
    }

and it keep gives me error such as:

 SymfonyComponentDebugExceptionFatalThrowableError  : Type error: Argument 1 passed to AppMailUserAutoDeleted::__construct() must be an instance of AppUser, instance of IlluminateSupportHigherOrderCollectionProxy given, called in C:laragonwwwmynewsiteappConsoleKernel.php on line 35

  at C:laragonwwwmynewsiteappMailUserAutoDeleted.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   AppMailUserAutoDeleted::__construct(Object(IlluminateSupportHigherOrderCollectionProxy))
      C:laragonwwwmynewsiteappConsoleKernel.php : 35

  2   AppConsoleKernel::AppConsole{closure}()
      C:laragonwwwmynewsitevendorlaravelframeworksrcIlluminateContainerBoundMethod.php : 29

  Please use the argument -v to see more details.

any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SOLVED

I put my mailing code to another loop function like:

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

and it's working just fine now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...