Home > Back-end >  Laravel check if user model implements email verification
Laravel check if user model implements email verification

Time:06-06

I want to check if email verification in laravel was enabled. The feature can be enabled by implementing the MustVerifyEmail interface on the user model.

But what is a proper laravel way to check if this feature was enabled?

Background: I want to create a command line user creation command that also sends an email verification link, but only if that feature is enabled.

CodePudding user response:

instanceof is used to determine whether a PHP variable is an instantiated object of a certain class.

So we use instanceof to check if User class implements the MustVerifyEmail interface:

if ($userObject instanceof MustVerifyEmail) {
    // Do what you want here
}

CodePudding user response:

create a method

public function isMustVerifyEmail():bool
{
  return ($this instanceof MustVerifyEmail);
}

you can use like bellow


$check=$user->isMustVerifyEmail();
dd($check);

CodePudding user response:

In case you are using fortify, which is almost secure.

I think that the correct way of check if email verification is enabled is checking Fortify config.

'features' => [
       
        Features::emailVerification(),

And you can check that with this line of code

in_array('email-verification',config('fortify.features'), true) ? 'email verification is enabled' : 'not enabled';
  • Related