-1

I have created an Event called UserWalletNewTransaction.php and added this to it:

public $transaction;

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

And also registered it at EventServiceProivder.php:

use App\Listeners\UserWalletNotification;

protected $listen = [
    UserWalletNewTransaction::class => [
        UserWalletNotification::class,
    ],

Now in order to fire this event at the Controller, I coded this:

$newTransaction = UserWalletTransaction::create(['user_id' => $user_id, 'wallet_id' => $wallet_id, 'creator_id' => $creator_id, 'amount' => $amount_add_value, 'description' => $trans_desc]);

event(new UserWalletNewTransaction($newTransaction));

Then at the listener, UserWalletNotification.php, I tried:

public function handle(UserWalletNewTransaction $event) {
    $uid = $event->user_id;
    dd($uid);
}

But I get Undefined property: App\Events\UserWalletNewTransaction::$user_id error message.

However, if I try dd($event), this result successfully come up:

enter image description here

So what's going wrong here? How can I get the user_id that already exists at $event?

I would really appreciate any idea or suggestion from you guys...

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43

2 Answers2

1

Try following, add this in your Event class UserWalletNewTransaction

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

and in Listener

public function handle(UserWalletNewTransaction $event) {
    $uid = $event->transaction->user_id;
    dd($uid);
}
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Nevermind23
  • 375
  • 1
  • 4
  • 14
  • Argument 1 passed to App\Events\UserWalletNewTransaction::__construct() must be an instance of App\Events\UserWalletTransaction, instance of App\UserWalletTransaction given, called in UserWalletController.php –  Jul 18 '21 at 08:20
  • You have to add at the beginning of your event class this code `use App\UserWalletTransaction;`. – matiaslauriti Jul 18 '21 at 08:24
1

The error is pretty clear, you are trying to access $user_id on an object that is App\Events\UserWalletNewTransaction and not your UserWalletTransaction model.

Your fix is:

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

If you use a good IDE, this would never happen to you, as it would already tell you that $event is UserWalletNewTransaction. Try using another IDE or one that can autocomplete that, so you can develop faster and better.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • `Trying to get property 'user_id' of non-object` –  Jul 18 '21 at 08:17
  • @loctoj then, you did not create the object... `UserWalletTransaction::create` did not return an object, maybe it was not added, I think it returned `false` or `null`, as your original image shows... this is basic debugging, do `dd($event->transaction)` and show us the result. – matiaslauriti Jul 18 '21 at 08:23
  • If the answer is `null`, then it is not creating the object, I think you may have not defined something in the model as `$fillable` property. Check that or create a new question as our code is correct, but you have a new problem now. I still have to say that you have some data in your image, I am really confused. – matiaslauriti Jul 18 '21 at 08:35