I am developing a web application using Laravel framework. I am trying to trying to use event and listener in my application. But the event was trigged and the listener for the trigged event is not fired.
This is my controller action
public function store(Request $request)
{
//other code
$item = Item::create($request->all())
broadcast(new ItemCreated($item));
return "Item created successfully";
}
This is my Events\ItemCreated.php
class ItemCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $item;
public function __construct($item)
{
$this->item = $item;
}
}
Then I have a listener for that event.
Listeners/EmailSubscribedUsers.php
class EmailSubscribedUsers
{
public function __construct()
{
//this constructor is not triggered
}
public function handle(ItemCreated $event)
{
//This method is not fired
}
}
In the EventServiceProvider I registered the event and the listener like this
protected $listen = [
ItemCreated::class => [
EmailSubscribedUsers::class
]
];
The event is trigged. But the listener is not fired. Why? What is wrong?
I tried the following solutions.
php artisan optimize
composer dumpautoload
php artisan clear-compiled