当我运行 Laravel Schedule 命令“php artisan Schedule:work”时,我收到此错误。正在跑步; 拉拉维尔 11 PHP 8.3
ArgumentCountError
Too few arguments to function App\Jobs\OrangeJob::__construct(), 0 passed in D:\projects\pulse\routes\console.php on line 8 and exactly 1 expected
at app\Jobs\OrangeJob.php:20
16▕ {
17▕ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
18▕ public $tries = 3;
19▕ public $orange;
➜ 20▕ public function __construct( $orange )
21▕ {
22▕ $this->orange=$orange;
23▕ }
24▕
1 routes\console.php:8
App\Jobs\OrangeJob::__construct()
2 vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php:499
不知道为什么我会收到这个错误。
这是我的控制器,我从那里调度工作
public function index()
{
$oranges = DB::connection('connectpay')
->table('oranges')
->whereYear('created_at', '=', date('Y'))
->where('created_at', '<', Carbon::now()->subMinutes(1)->toDateTimeString())
->where('status', '=', 'pending')->get();
foreach ($oranges as $orange) {
OrangeJob::dispatch($orange);
}
}
我的 Laravel 的工作类别
<?php
namespace App\Jobs;
use App\Models\Test;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use function Laravel\Prompts\table;
class OrangeJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 3;
public $orange;
public function __construct( $orange )
{
$this->orange=$orange;
}
public function handle():void
{
$orange= $this->orange;
// we check our transactions status
$this->checkOrangeTransactions($orange);
}
这是我的路线/控制台文件,我想在其中安排工作
<?php
use App\Jobs\OrangeJob;
Schedule::job(new OrangeJob())->everySecond();
提前致谢
在此请向 OrangeJob() 传递一些参数
Schedule::job(new OrangeJob())->everySecond();
修改后的代码如下:
$someArgument = "some data or dataset";
Schedule::job(new OrangeJob($someArgument))->everySecond();
OrangeJob 正在等待参数。调度参数时不通过 OrangeJob 传递。
$orange = [
id: "ewdsdmsmd",
user_id : 2
];
Schedule::job(new OrangeJob($orange))->everySecond();