gpt4 book ai didi

laravel - 执行事件时调用 Laravel 中未定义的方法

转载 作者:行者123 更新时间:2023-12-04 15:12:14 24 4
gpt4 key购买 nike

我在发帖时遇到了这个错误并且它触发了我的事件

Call to undefined method App\Events\UpdateProductCount::__invoke()


这是我的代码
    public function create()
{
$product = Product::create([
'name' => request('name'),
'description' => request('description'),
]);

dispatch(new UpdateProductCount($product));
}
这就是我在 UpdateProductCount 事件中的内容
    <?php

namespace App\Events;

use App\Models\Product;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class UpdateProductCount
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public Product $product;

public function __construct(Product $product)
{
$this->product = $product;
}

public function broadcastOn()
{
return new PrivateChannel('product-count-update'.$this->product);
}
}
这就是我在 UpdateProductCountListener 中的内容
    <?php

namespace App\Listeners;

use App\Events\UpdateProductCount;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class UpdateProductCountListener
{

public function __construct()
{

}

public function handle(UpdateProductCount $event)
{
dd($event);
}
}

最佳答案

像@miken32 一样查看了我的堆栈跟踪后,建议它提示这一行 dispatch(new UpdateProductCount($product));所以我尝试改变 dispatch(new UpdateProductCount($product));到 event(new UpdateProductCount($product));` 并且有效。

关于laravel - 执行事件时调用 Laravel 中未定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64998793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com