Laravel-modules: Listeners in modules not support ShouldQueue ?

Created on 20 Jun 2019  路  4Comments  路  Source: nWidart/laravel-modules

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!

All 4 comments

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());
    }

Source:
https://laracasts.com/discuss/channels/laravel/serialization-of-closure-is-not-allowed-in-queuing-a-mail?page=1

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());
    }

Source:
https://laracasts.com/discuss/channels/laravel/serialization-of-closure-is-not-allowed-in-queuing-a-mail?page=1

Thank You very much it Works!, I did not realize that I was passing the full object Request in my constructor

Was this page helpful?
0 / 5 - 0 ratings

Related issues

morningmemo picture morningmemo  路  3Comments

pawlox picture pawlox  路  4Comments

dagulo picture dagulo  路  3Comments

vdjkelly picture vdjkelly  路  4Comments

quentingosset picture quentingosset  路  4Comments