首页 > 解决方案 > Laravel 使用方法 when() 安排作业

问题描述

以下代码中的错误在哪里?在终端中发出php artisan schedule:run命令时出现致命错误。

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
        $forgotCheckout = Working::whereNull('deleted_at')->get();
        $forgot = [];
            foreach($forgotCheckout as $forgot){
                $forgot;
            }
            Mail::send(
                'emails.forgot_checkout',
                compact('forgot'),
                function ($message) use ($forgot) {
                    $message->to('test@email.com');
                    $message->subject('This is test mail');

                }
            );
    })->daily()->when(function ($forgot){
        if(is_null($forgot)){
            return false;
        }
        else{
            return true;
        }
    });
}

Symfony\Component\Debug\Exception\FatalThrowableError :类型错误:函数 App\Console\Kernel::App\Console{closure}() 的参数太少,通过了 0,预期正好 1

  at C:\www\test\app\Console\Kernel.php:50
    46|                         $message->subject('This is test mail');
    47|
    48|                     }
    49|                 );
  > 50|         })->everyMinute()->when(function ($forgot){
    51|             if(is_null($forgot)){
    52|                 return false;
    53|             }
    54|             else{

标签: phplaravel

解决方案


试试下面的代码。

protected function schedule(Schedule $schedule)
{
    $forgot = [];
    $schedule->call(function () {
        $forgotCheckout = Working::whereNull('deleted_at')->get();

            foreach($forgotCheckout as $forgot){
                $forgot;
            }
            Mail::send(
                'emails.forgot_checkout',
                compact('forgot'),
                function ($message) use ($forgot) {
                    $message->to('test@email.com');
                    $message->subject('This is test mail');

                }
            );
    })->daily()->when(function() use($forgot) {
        if(is_null($forgot)){
            return false;
        }
        else{
            return true;
        }
    });
}

更改的代码行:

->daily()->when(function() use($forgot) {

推荐阅读