Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andreasb's avatar

Model Event - and non-static methods

Good evening,

I just went through the E-Mail Verification video and adjusted it to my needs.

Now in my User-Model I am trying to setup the following Model Events:

    public static function boot()
    {
        parent::boot();

        static::creating(function ($user)
        {
            $user->emailToken = str_random(30);
        });

        User::created(function ($user)
        {
           UserMailer::sendConfirmation($user);
        });
    }

UserMailer::sendConfirmation looks like this:

class UserMailer extends Mailer {

    public function sendConfirmation(User $user)
    {
        $view = 'mails.confirmation';
        $data = [];
        $subject = "Please confirm test 1 2 3 ";

        return $this->sendTo($user, $view, $data, $subject);

    }

}

Of course, I receive the following error when I try to run this:

Non-static method App\Mailers\UserMailer::sendConfirmation() should not be called statically

If I set the sendConfirmation method as static, it obviously cannot call on $this.

Since I am very confused at the moment - how do I best approach this?

Thank you Andreas

0 likes
2 replies
nolros's avatar
nolros
Best Answer
Level 23

Andreas, the best way to do this is to create an event, example below, but I assume you could access it with self:: e.g.

$token = self::emailToken = str_random(30);

Example of event approach:

   public static function boot()
    {
        parent::boot();

        // Attach event handler, creation of the user
        User::created(function($user)
        {
            event(new UserHasRegistered($user));

        });
    }

Then create an event called UserHasRegistered and a listener Mailer listens for the event that sends the mail.

Please or to participate in this conversation.