Php 计划运行时无法调度Laravel事件
每当调度命令成功运行时,我想触发一个名为ThriftStarted的事件。事件侦听器GeneratePaymentSchedule应处理事件并生成付款计划。但我似乎遗漏了一些东西,因此当计划运行时,不会按预期生成付款计划 下面是我的代码 Kernel.phpPhp 计划运行时无法调度Laravel事件,php,laravel,laravel-5.8,Php,Laravel,Laravel 5.8,每当调度命令成功运行时,我想触发一个名为ThriftStarted的事件。事件侦听器GeneratePaymentSchedule应处理事件并生成付款计划。但我似乎遗漏了一些东西,因此当计划运行时,不会按预期生成付款计划 下面是我的代码 Kernel.php <?php namespace App\Console; use App\Thrift; use App\Events\ThriftStarted; ... class Kernel extends ConsoleKernel
<?php
namespace App\Console;
use App\Thrift;
use App\Events\ThriftStarted;
...
class Kernel extends ConsoleKernel
{
...
protected function schedule(Schedule $schedule)
{
// start thrift plan
$schedule->call(function() {
// get all open thrift plans
$thrifts = Thrift::where('open', true)->get();
// set open to false if contributors equals subscribers
foreach ($thrifts as $thrift) {
if ($thrift->contributors === $thrift->subscriptions->count()) {
$thrift->update(['open' => false]);
// dispatch thriftStarted event
event(new ThriftStarted($thrift));
}
}
})->everyMinute();
}
...
}
一些基本的调试怎么样。您的日志中有错误吗?命令是否正常运行?它的schedule()
方法是否运行?调用event()
是否发生?如果是这样,听者是否能识别它?将您的问题缩小到导致问题的组件,您可能会发现自己正在解决过程中的问题。您是否已将App\Events\ThriftStarted::$thrift
声明为公共属性?它不在您提供的代码中。日志中没有错误。计划成功运行。我还在ThriftStarted
事件中声明了public$thrift
(将在此处编辑代码)。但是监听器仍然无法处理,也许你忘记了调度程序。添加cron作业***cd/path to your project&&php artisan schedule:run>/dev/null 2>&1
我仍在本地系统上开发,所以这不是问题所在
<?php
namespace App\Events;
use App\Thrift;
...
class ThriftStarted
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Thrift $thrift)
{
$this->thrift = $thrift;
}
...
}
<?php
namespace App\Listener;
use App\Payment;
use App\Events\ThriftStarted;
...
class GeneratePaymentSchedule
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param ThriftStarted $event
* @return void
*/
public function handle(ThriftStarted $event)
{
if ($event->thrift->open === 0) {
$subs = Subscription::where('thrift_id', $event->thrift->id)->get();
$schedule = Carbon::parse(now())->toDateString();
foreach ($subs as $sub) {
Payment::create([
'user_id' => $sub->user_id,
'thrift_id' => $sub->thrift_id,
'contribution' => $sub->thrift->contribution,
'schedule' => $schedule,
]);
$schedule = Carbon::parse($schedule)->addMonth()->toDateString();
}
}
}
}
<?php
...
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
App\Events\ThriftStarted::class => [
App\Listener\GeneratePaymentSchedule::class,
]
];
...
}