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

Emokores's avatar

MustVerifyEmail trait not working

My User model implements the MustVerifyEmail trait, and it was working perfectly until I added an if block in my Fortify CreateNewUser class that first checks if there are any records in the User model:


public function create(array $input)
{
     Validator::make($input, [
         //* Validation
     ])->validate();

    //* first check if model has any records. I just added this `if` block:
    if (User::count() < 1) {
            return User::create([
                'name' => $input['fname'] . ' ' . $input['lname'],
                'email' => $input['email'],
                'gender' => $input['gender'],
                'mobile_number' => $input['mobile'],
                'password' => Hash::make($input['password']),
                'is_active' => 1
           ])->roles()->attach([1, 2, 3]);
     }

     return User::create([
        'name' => $input['fname'] . ' ' . $input['lname'],
        'email' => $input['email'],
        'gender' => $input['gender'],
        'mobile_number' => $input['mobile'],
        'password' => Hash::make($input['password']),
    ]);
}

Now when the first user registers, I get the error: Illuminate\Auth\SessionGuard::login(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, null given which was not happening before. The second, third and other registering users are sent the email verification link after registration, which is what I want for all registering users. How can I fix this so that even the first user verifies his email?

0 likes
3 replies
Nakov's avatar
Nakov
Best Answer
Level 73

Replace your if condition with this

if (User::count() < 1) {
            $user = User::create([
                'name' => $input['fname'] . ' ' . $input['lname'],
                'email' => $input['email'],
                'gender' => $input['gender'],
                'mobile_number' => $input['mobile'],
                'password' => Hash::make($input['password']),
                'is_active' => 1
           ]);

$user->roles()->attach([1, 2, 3]);

return $user;
}
1 like
smacy's avatar
'features' => [
    Features::registration(),
    Features::resetPasswords(),
    Features::emailVerification(), 
    Features::updateProfileInformation(),
    Features::updatePasswords(),
    Features::twoFactorAuthentication([
        'confirmPassword' => true,
    ]),
],

class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, Notifiable;

why User::count()?

if (User::count() < 1) {
            $user = User::create([
                'name' => $input['fname'] . ' ' . $input['lname'],
                'email' => $input['email'],
                'gender' => $input['gender'],
                'mobile_number' => $input['mobile'],
                'password' => Hash::make($input['password']),
                'is_active' => 1
           ]);

$user->roles()->attach([1, 2, 3]);

return $user;
}

Please or to participate in this conversation.