2

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?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
japose7523
  • 29
  • 2
  • 15
  • If it's the same object, then you could create a data member, store `$user->id` into it and refer to that value when the second handler is executed. – Lajos Arpad Nov 16 '22 at 12:34
  • @LajosArpad Can you please write what you mean as code, so it will be understandable – japose7523 Nov 16 '22 at 13:00
  • I don't have Laravel in front of me to test, this is why I have not written an answer. But, as an idea, if you have an object that is seen by both handlers and you also know what its class is (let's call it `Foo` for now), then you can create a `protected $id` data-member for that class along with a getter `public function getId() {return $this->id;}` and a setter `public function setId($id) {$this->id = $id;}`. You can call `setId` from your first handler, passing the correct value and retrieve it in the second handler via calling `getId`. – Lajos Arpad Nov 17 '22 at 15:07

1 Answers1

2

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,
    ]);
}
HijenHEK
  • 1,256
  • 1
  • 4
  • 13