How to check if a user email already exists in Laravel?


There are many ways you can test if email exists. One way is using the validator class. To make use of validator you need to include the class as shown below;

use Illuminate\Support\Facades\Validator;

Example 1

The example shows how to make use of Validator to check if email is registered or not.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class UserController extends Controller { public function index() { $inputValues['email'] = "heena@gmail.com"; // checking if email exists in ‘email’ in the ‘users’ table $rules = array('email' => 'unique:users,email'); $validator = Validator::make($inputValues, $rules); if ($validator->fails()) { echo 'The email already exists'; } else { echo 'The email is not registered'; } } }

Output

The output of the above code is −

The email already exists

Example 2

Now let us try with an email which is not present inside the users table.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class UserController extends Controller{ public function index() { $inputValues['email'] = "test@gmail.com"; // checking if email exists in ‘email’ in the ‘users’ table $rules = array('email' => 'unique:users,email'); $validator = Validator::make($inputValues, $rules); if ($validator->fails()) { echo 'The email already exists'; } else { echo 'The email is not registered'; } } }

Output

The output of the above code is −

The email is not registered

Example 3

You can make use of Eloquent model to check if email exists inside the users table

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller{ public function index() { $email = "heena@gmail.com"; $userEmailDetails = User::where('email', '=', $email)->first(); if ($userEmailDetails === null) { echo 'The email is not registered'; } else { echo 'The email already exists'; } } }

Output

The output of the above code is −

The email already exists

Example 4

Using the count() method from Laravel eloquent model −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function index() { $email = "heena@gmail.com"; if (User::where('email', '=', $email)->count() > 0) { echo "Email Exists"; } else { echo "Email is not registered"; } } }

Output

The output of the above code is −

Email Exists

Example 5

Using the exists() method from Laravel eloquent model −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller{ public function index() { $email = "heena@gmail.com"; if (User::where('email', '=', $email)->exists()) { echo "Email Exists"; } else { echo "Email is not registered"; } } }

Output

The output of the above code is −

Email Exists

Updated on: 30-Aug-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements