phplaravellaravel-5.8laravel-events

Laravel 5.8: How to pass two arguements to Event & Listener


I'm using Laravel 5.8 and I have created an Event called UserWalletNewTransaction that goes like this:

public $transaction;
public $added_type;

public function __construct($transaction, $added_type)
{
    $this->transaction = $transaction;
    $this->added_type = $added_type;
}

As you can see I have specified two parameters here and these parameters are getting their value from the Controller:

event(new UserWalletNewTransaction($newTransaction, $value_added_type));

And then the Listener which is named UserWalletNotification goes like this:

public function handle(UserWalletNewTransaction $event, $added_type) {

But there is something wrong here, since I'm getting this error:

Too few arguments to function App\Listeners\UserWalletNotification::handle(), 1 passed and exactly 2 expected

So how to fix this issue? How can I pass two parameters to Event & Listener properly?


Update 1

Error Screenshot:

enter image description here


Solution

  • UserWalletNewTransaction event:

    public $transaction;
    public $added_type;
    
    public function __construct($transaction, $added_type)
    {
        $this->transaction = $transaction;
        $this->added_type = $added_type;
    }
    

    which you can call like this event(new UserWalletNewTransaction($newTransaction, $value_added_type));

    and UserWalletNotification listener:

    public function handle(UserWalletNewTransaction $event)
    {
        $transaction = $event->transaction;
        $added_type = $event->added_type;
    }