Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...