0

In my User model I can do:

public function sendPasswordResetNotification($token)
{
  $message = (new UserForgotPassword($this->email, $this->name, $token));
}

but how can I pass the full User object?

public function sendPasswordResetNotification($token)
{
  $message = (new UserForgotPassword($user, $token));
}

Using $user doesn't work, while it does work in:

protected static function booted()
{
  static::creating(function ($user) {
    $user->uuid = Str::uuid();
  });
}
5
  • just use this instead, $message = (new UserForgotPassword($this, $token)); Commented Aug 9, 2020 at 11:47
  • Didn't know I could do that! Commented Aug 9, 2020 at 11:48
  • 1
    yea, u could, let me know if you face any prob :)) Commented Aug 9, 2020 at 11:50
  • @Abdel-azizhassan if you post that as an answer I can accept it :-) Commented Aug 9, 2020 at 13:03
  • Okay, I will thanks :)) Commented Aug 9, 2020 at 13:06

2 Answers 2

1

just use this instead,

$message = (new UserForgotPassword($this, $token));
Sign up to request clarification or add additional context in comments.

Comments

0

Use $this to have access to the User instance inside the User model (in a non-static method).

public function sendPasswordResetNotification($token)
{
  $message = (new UserForgotPassword($this, $token));
}

Note: If your method was a static method, you would use static (or self) instead of $this.

Also, in the example you gave, $user is not actually a variable in the booted method. It is rather a parameter of the closure (the anonymous function) passed to the created method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.