I'm trying to implement "ShouldQueue" functionality in my event listener as Laravel Documentation but I think there is an issue because I got an error: Serialization of 'Closure' is not allowed when I try to Implement ShouldQueue, I remove all my logic inside this listener to test if something inside is wrong but even if is empty the handle method is crashing
namespace Modules\Payments\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
use Modules\Payments\Emails\PaymentReceiptMail;
Class SendPaymentReceiptListener implements ShouldQueue --> **this does not work**
{
public function __construct()
{
}
public function handle($event)
{
//Mail::to($event->request->email)->send(new PaymentReceiptMail());
}
}
If I try with a normal event and listener native of Laravel inside App\Listeners, everything works fine, the problem is inside the module when I try to use ShouldQueue
Could you give me an idea of what is happening?
Thank You!
Can you show the event implementation?
Can you show the event implementation?
Yes sure this the Event:
namespace Modules\Payments\Events;
use Illuminate\Queue\SerializesModels;
class NewPaymentHasBeenProcessedEvent
{
use SerializesModels;
public $request;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($request)
{
$this->request = $request;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}
And this is the EventService Provider:
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
NewPaymentHasBeenProcessedEvent::class => [
SendPaymentReceiptListener::class,
],
];
}
@Kyslik @mikemand
Don't pass $request to the constructor. If u need get an request value, try to pass the value instead full $request object or pass all request values as array.
Change:
public function __construct($request)
{
$this->request = $request;
}
To:
public function __construct($request)
{
$this->toEmail = $request->email;
// or $this->request = $request->all();
}
And change on Listener:
public function handle($event)
{
//Mail::to($event->request->email)->send(new PaymentReceiptMail());
}
To:
public function handle($event)
{
Mail::to($event->toEmail)->send(new PaymentReceiptMail());
// or Mail::to($event->request['email'])->send(new PaymentReceiptMail());
}
Don't pass
$requestto the constructor. If u need get an request value, try to pass the value instead full$requestobject or pass all request values as array.Change:
public function __construct($request) { $this->request = $request; }To:
public function __construct($request) { $this->toEmail = $request->email; // or $this->request = $request->all(); }And change on Listener:
public function handle($event) { //Mail::to($event->request->email)->send(new PaymentReceiptMail()); }To:
public function handle($event) { Mail::to($event->toEmail)->send(new PaymentReceiptMail()); // or Mail::to($event->request['email'])->send(new PaymentReceiptMail()); }
Thank You very much it Works!, I did not realize that I was passing the full object Request in my constructor