I'm working with Laravel 9 and for registration of my website, I tried adding this event as the register form action:

$data = $request->validated();

event(new RegisterNewUserEvent($data));

And here is the RegisterNEwUserEvent:

class RegisterNewUserEvent implements ShouldQueue
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public mixed $data;

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

And this event has assigned with these two listeners:

        RegisterNewUserEvent::class => [
            MakeNewUserRecord::class,
            MakeNewMemberRecord::class
        ],

MakeNewUserRecord:

class MakeNewUserRecord implements ShouldQueue
{
    public function handle(RegisterNewUserEvent $event)
    {
        $firstName = $event->data['fname'];
        $lastName = $event->data['lname'];
        $userName = $event->data['uname'];
        $mobilePhone = $event->data['phone'];
        $hashPassword = Hash::make($event->data['password']);
        $randomString = Str::random(20);

        $user = User::create([
            'usr_first_name' => $firstName,
            'usr_last_name' => $lastName,
            'usr_user_name' => $userName,
            'usr_mobile_phone' => $mobilePhone,
            'usr_password_hash' => $hashPassword,
            'usr_str' => $randomString,
        ]);

        $event->data = $user->id;
    }
}

MakeNewMemberRecord:

class MakeNewMemberRecord implements ShouldQueue
{
    public function handle(RegisterNewUserEvent $event)
    {
        $member = Member::create([
            'mbr_usr_id' => $event->data,
            'mbr_type_id' => 7,
        ]);
    }
}

These event listeners work fine but as soon as I tried implementing ShouldQueue to the event class and the listeners classes, I get this error when register new user:

Illuminate\Database\Grammar::parameterize(): Argument #1 ($values) must be of type array, int given, called in C:\project\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 1021

enter image description here

And I follow the error stack, I can see that this error is returned when the event is called:

enter image description here

I don't know really what's going wrong here! So if you know, please let me know...

Thanks.

1

There are 1 answers

0
Chinh Nguyen On BEST ANSWER

since both listeners are Queueable, its data will be serialized and stored somewhere (for example: in the database, the place depends on queue connection), so you can't modify or change the event data after it has been dispatched which means $event->data will always be an array in every listener

my suggestion: remove MakeNewMemberRecord from current event, create a new event (RegisterNewMemberEvent) for that listener, and dispatch RegisterNewMemberEvent after creating the user

    RegisterNewUserEvent::class => [
        MakeNewUserRecord::class,
    ],
    RegisterNewMemberEvent::class => [
        MakeNewMemberRecord::class,
    ],
class MakeNewUserRecord implements ShouldQueue
{
    public function handle(RegisterNewUserEvent $event)
    {
        $firstName = $event->data['fname'];
        $lastName = $event->data['lname'];
        $userName = $event->data['uname'];
        $mobilePhone = $event->data['phone'];
        $hashPassword = Hash::make($event->data['password']);
        $randomString = Str::random(20);

        $user = User::create([
            'usr_first_name' => $firstName,
            'usr_last_name' => $lastName,
            'usr_user_name' => $userName,
            'usr_mobile_phone' => $mobilePhone,
            'usr_password_hash' => $hashPassword,
            'usr_str' => $randomString,
        ]);

        RegisterNewMemberEvent::dispatch($user->id);
    }
}
class RegisterNewMemberEvent implements ShouldQueue
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public int $userId;

    public function __construct($userId)
    {
        $this->userId = $userId;
    }
}
class MakeNewMemberRecord implements ShouldQueue
{
    public function handle(RegisterNewMemberEvent $event)
    {
        $member = Member::create([
            'mbr_usr_id' => $event->userId,
            'mbr_type_id' => 7,
        ]);
    }
}

Note: i guess return false inside handle method won't stop the propagation of an event since you are using queue so the above implementation will make it possible to stop other listeners to be run