Seah Sky
Seah Sky

Reputation: 154

Laravel MustVerifyEmail is not sending e-mail

I'm currently working on a project with Laravel 5.7. As I implemented the MustVerifyEmail class in my User model, the application does not send email as expected. The case is:

1) I have followed the Laravel documentation here: https://laravel.com/docs/5.7/verification

2) I used https://mailtrap.io to test the function, but I didn't receive any verification email from the application.

3) I tried to use Auth::routes() instead of Auth::routes(['verify' => true]) and was expecting errors, but no error occurred, the application just redirected me to the home page after the user registration.

Here's my User model:

<?php
namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name', 'last_name', 'email', 'password', 'phone_number', 'username', 'role'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token'
    ];

    public function freelancer()
    {
        return $this->hasOne('App\Freelancer', 'freelancer_id', 'id');
    }

    public function employer()
    {
        return $this->hasOne('App\Employer', 'employer_id', 'id');
    }
}

This is my create() function in RegisterController.php:

protected function create(array $data)
{
    if ($data['role'] == 'freelancer') {
        $user = User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'phone_number' => $data['phone_number'],
            'username' => $data['username'],
            'password' => Hash::make($data['password']),
            'role' => $data['role'],
        ])->freelancer()->create([
            'overview_description' => '',
        ]);
    } else {
        $user = User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'phone_number' => $data['phone_number'],
            'username' => $data['username'],
            'password' => Hash::make($data['password']),
            'role' => $data['role'],
        ])->employer()->create([
            'overview_description' => '',
            'number_of_employees' => 0,
        ]);
    }

    return $user;
}

My Freelancer and Employer model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Freelancer extends Model
{
    protected $table = 'freelancer';

    public $timestamps = false;

    protected $fillable = [
      'overview_description'
    ];
}

--

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Employer extends Model
{
    protected $table = 'employer';

    public $timestamps = false;

    protected $fillable = [
      'overview_description', 'number_of_employees'
    ];
}

I think there is something wrong in the RegisterController, but I'm not sure where it is. Is there any logic error?

Upvotes: 2

Views: 4280

Answers (2)

Seah Sky
Seah Sky

Reputation: 154

The issue solved after I updated the create function in RegisterController.php as follows:

  protected function create(array $data)
  {
     $user = User::create([
    'first_name' => $data['first_name'],
    'last_name' => $data['last_name'],
    'email' => $data['email'],
    'phone_number' => $data['phone_number'],
    'username' => $data['username'],
    'password' => Hash::make($data['password']),
    'role' => $data['role'],
    ]);

    return $user;
  }

As for the creation process of the Freelancer and Employer instances, I placed it after the email verification.

Upvotes: 1

Yves Kipondo
Yves Kipondo

Reputation: 5613

You forget to use the Illuminate\Auth\MustVerifyEmail trait in your User model, this trait define the sendEmailVerificationNotification method which is responsible for sending the verification email.

use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract; // this is an interface
use Illuminate\Auth\MustVerifyEmail; // this the trait

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

    // all the other code goes here

}

Upvotes: 3

Related Questions