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

julianov's avatar

VerifyEmail Laravel 9 and crud API

Hello everyone, I'm doing my first steps with Laravel and I want to implement a CRUD api to register a user. The client is on a Node.js server so I can't make use of resources in laravel.

If there is a POST request, where the user fields arrive, I have to send an email to the user to confirm it. What is the best way to do this?

What I mean is that I can save the values ​​in the database and know if they registered their email or not if the confirmed field is complete in the DB table

This is the migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
   
    public function up()
    {
        Schema::create('ciudadanos', function (Blueprint $table) {
            
            $table->id();
            $table->bigInteger('cuil')->unique();
            $table->string('nombre');
            $table->string('apellido');
            $table->string('email');
            $table->string('password');

            $table->timestamp('email_verified_at')->nullable();
            $table->boolean('confirmed')->default(0);
            $table->string('confirmation_code')->nullable();

            $table->rememberToken();
            $table->timestamps();
            
        });
    }
    public function down()
    {
        Schema::dropIfExists('ciudadanos');
    }
 }

This are the fields I'd use to implement verify email:

 $table->timestamp('email_verified_at')->nullable();
 $table->boolean('confirmed')->default(0);
 $table->string('confirmation_code')->nullable();

So, in the controller store method:

public function store(Request $request)
    {    
        $this->validate($request, [
            'cuil'=>'required',
            'nombre'=>'required',
            'apellido'=>'required',
            'email'=>'required',
            'password'=>'required',
        ]);
       
        $ciudadano = Ciudadano::create($validated);

        //new code to confirm email

        $ciudadano->confirmation_code = rand(500, 15000);
        send_email($ciudadano->confirmation_code);

        $ciudadano->save();
        return ("OK");
    }

There is this lines:

        $ciudadano->confirmation_code = rand(500, 15000);
        send_email($ciudadano->confirmation_code);

Then, upon receiving OK, the user will show a screen where they must enter the code that came by email and there the field is updated $table->boolean('confirmed')->default(0); in the DB

What do you think? It's a good way to do it?

Thanks in advance

0 likes
6 replies
julianov's avatar

@Sinnbeck Thanks but I don't want to add a verification view in resources/views directory

Sinnbeck's avatar

@julianov but you still want a page? You can return view/page you want. It goes to /email/verify

Be aware that the user does not need to enter anything. Instead the link in the mail has a secret token which is used to guarantee its the right user. So a slightly different approach, but it's what most sites do

julianov's avatar

@Sinnbeck Thanks sir. I have a last question, Because I have this console output:

PHP Fatal error: Class App\Models\Ciudadano contains 4 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, ...) in 
  app/Models/Ciudadano.php on line 15.

this is the model class:

<?php

namespace App\Models;


use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;


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

class Ciudadano extends Model implements MustVerifyEmail
{ 

    use Notifiable;
    
    protected $table = 'ciudadanos';

    protected $fillable =[ 
        'cuil',
        'nombre',
        'apellido',
        'email',
        'password',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

The laravel documentation does not say anything about that methods. Why I have that problem?

Sinnbeck's avatar

Or add this trait Illuminate\Auth\MustVerifyEmail

Please or to participate in this conversation.