Artisan 命令给出错误 'Object of class Closure could not be converted to string'

Artisan command gives error 'Object of class Closure could not be converted to string'

我想在 Mac 上使用 Laravel 中的 Schedule。这是我的 crod 文件:

SHELL=/bin/bash PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin * * * * * php /Applications/MAMP/htdocs/xxxy/xxyyy/artisan schedule:run >> $run >> /dev/null 2>&1

这是Kernel.php

namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
protected $commands = [
     //Commands\Inspire::class,
];

protected function schedule(Schedule $schedule)
{   
    $schedule->command(function () {
        dispatch(new \App\Jobs\sendPush);
    })->everyMinute();        
}
}

我用 artisan 在终端中输入的任何内容都会出现此错误:

[ErrorException] Object of class Closure could not be converted to string

你知道如何让 Artisan 工作吗?

sendPush.php

class sendPush extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public function __construct()
{
    //
}
public function handle(MessageController $MessageController)
{
    $MessageController->sendDefault();
}

我以前从未使用过这些,但快速浏览 docs 似乎突出了您的问题:

You may define all of your scheduled tasks in the schedule method of the App\Console\Kernel class. To get started, let's look at an example of scheduling a task. In this example, we will schedule a Closure to be called every day at midnight. Within the Closure we will execute a database query to clear a table:

In addition to scheduling Closure calls, you may also schedule Artisan commands and operating system commands. For example, you may use the command method to schedule an Artisan command using either the command's name or class:

似乎您需要使用 call 方法而不是使用 command 方法,因为您传入的是 Closure 而不是命令名称或 class .

所以我相信您只需将代码更新为以下内容:

protected function schedule(Schedule $schedule) {   
    $schedule->call(function () {
        dispatch(new \App\Jobs\sendPush);
    })->everyMinute();        
}