How to pass the object from a listener of an event to another listener of the same event

842 views Asked by At

In Laravel 9, I have created an Event called RegisterNewUserEvent with this __construct function ($data here holds the $request data of a form).

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

And this event has two registered listeners:

protected $listen = [
        \App\Events\RegisterNewUserEvent::class => [
            \App\Listeners\MakeNewUserRecord::class
            \App\Listeners\MakeNewMemberRecord::class
        ]
    ];

One makes a new user record at the users table.

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,
    ]);

    // return $user;
}

And the other one makes a new record at the members table.

public function handle(RegisterNewUserEvent $event)
{
    $member = Member::create([
        'mbr_usr_id' => $user->id,
    ]);
}

So as you can see, the members table has a column named mbr_usr_id, which is connected to the users. id table column, and the value of this column must be assigned to the related user_id, created by the 1st listener. Therefore I need some way to pass the $user->id to the 2nd listener after the 1st listener is called. So how can I pass this new object, ' $user`, to another listener of the same event?

1

There are 1 answers

1
HijenHEK On BEST ANSWER

You can change the data value in the event from the first listener since the data is already a public event class variable , then the second listener will get the new value of data .

  1. we will override the $data variable with the created user id in the first listener $event->$data = $user->id;
  2. now in the second listener we can use the new data value which is the created user id

main event

public mixed data;

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

first listener

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,
    ]);
    
    // here is the line change
    $event->data = $user->id;
}

second listener

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