Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel使用方法when()调度作业_Php_Laravel - Fatal编程技术网

Php Laravel使用方法when()调度作业

Php Laravel使用方法when()调度作业,php,laravel,Php,Laravel,下面代码中的错误在哪里? 当我在终端中发出php-artisan-schedule:run命令时,出现了致命错误 protected function schedule(Schedule $schedule) { $schedule->call(function () { $forgotCheckout = Working::whereNull('deleted_at')->get(); $forgot = []; fo

下面代码中的错误在哪里? 当我在终端中发出
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{

请尝试以下代码

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) {
如果查看,则方法
when
运行
Callable
对象时,不传递任何参数

相反,您的代码需要一个参数:

})->everyMinute()->when(function ($forgot){
因此,您必须更改代码才能使用
$forget
变量(在您的方法中对其进行初始化),并按如下方式更改代码:

})->everyMinute()->when(function () use($forgot){

$forget
从未在上一个匿名文件的范围之外定义过function@lagbox:您的答案是正确的,我在
}->daily()->when(function()use($forget)中得到了未定义的变量错误{
,我该怎么解决呢?@Magnetic试图在方法的开头定义变量。@JamalAbdulNasir:我用你的答案编辑了,但是我得到了这个
没有计划好的命令可以运行。
如何解决它?@JamalAbdulNasir.没有!!--;你对
$forget
的初衷是什么它只是通过foreach循环在匿名函数中重新分配它