phplaravelmongodbemail-verificationjenssegers-mongodb

Following the steps to implement Laravel native mail verification bring to an issue


Following the guide to implement the native mail verification of laravel. Brings me an error.

Note please that i use MongoDB, therefore i'm using Jensseger/laravel-mongodb package

This is the error: Class App\User contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Contracts\Auth\MustVerifyEmail::hasVerifiedEmail, Illuminate\Contracts\Auth\MustVerifyEmail::markEmailAsVerified, Illuminate\Contracts\Auth\MustVerifyEmail::sendEmailVerificationNotification

I've already try to implement the methods inside my model and they seem to solve the problem. But it won't send any emails.

Here's what i've implemented im my User.php model

    * Determine if the user has verified their email address.
    *
    * @return bool
    */
    public function hasVerifiedEmail()
    {}

    /**
    * Mark the given user's email as verified.
    *
    * @return bool
    */
    public function markEmailAsVerified()
    {}

    /**
    * Send the email verification notification.
    *
    * @return void
    */
    public function sendEmailVerificationNotification()
    {}

Here's my User.php model

namespace App;

use App\Company;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Jenssegers\Mongodb\Auth\User as Authenticatable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    protected $connection = 'mongodb';

Here's my web.php route file.

Route::get('/', function () {
    return view('welcome');
});

Auth::routes(['verify' => true]);

Route::get('/home', 'HomeController@index')->name('home');

And here's my HomeController.php

    public function __construct()
    {
        $this->middleware(['auth','verified']);
    }

Here's my env file

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=25
MAIL_USERNAME=xxxxxxxxxxx
MAIL_PASSWORD=xxxxxxxxxxxx
MAIL_ENCRYPTION=tls

Like this the project work but it wont send emails. Do i need to put the logic inside the three method inside the User.php? If yes what should i put in it? I've no idea because if it's native and work like charm with SQL i don't really know how to get it work on my project Hope someone has a solution for this. Thanks


Solution

  • Easiest solution is to implement trait Illuminate\Auth\MustVerifyEmail which should be there, however it is not mentioned in the Laravel documentation. You can also override these methods by defining them in the model as you did. But hasVerifiedEmail and markEmailAsVerified methods should have some verification logic and return bool based on the API.

    Edit: I also forgot to mention that method sendEmailVerificationNotification should contain $this->notify(new Notifications\VerifyEmail); otherwise it won't use the Notifiable trait and thus not send any email. For more details take a look at the method in Laravel framework repository,