How to check if a user email already exist

0 votes
In laravel, when a new user is registering to my site and the email they use already exist in the database. how can tell the user that the email already exist ?
Dec 6, 2020 in Laravel by kartik
• 37,490 points
96 views

1 answer to this question.

0 votes

Hello @kartik,

The validation feature built into Laravel lets you check lots of things, including if a value already exists in the database. Here's an overly simplified version of what you need. In reality you'd probably want to redirect back to the view with the form and show some error messages.

// Get the value from the form
$input['email'] = Input::get('email');

// Must not already exist in the `email` column of `users` table
$rules = array('email' => 'unique:users,email');

$validator = Validator::make($input, $rules);

if ($validator->fails()) {
    echo 'That email address is already registered. You sure you don\'t have an account?';
}
else {
    // Register the new user or whatever.
}

);

Laravel has built-in human readable error messages for all its validation. You can get an array of the these messages via: $validator->messages();

Hope it helps!!

Thank You!!

answered Dec 6, 2020 by Niroj
• 82,560 points

Related Questions In Laravel

0 votes
1 answer

How to check if connected to database in Laravel?

Hello @kartik, You can use if(DB::connection()->getDatabaseName()) { echo ...READ MORE

answered Sep 27, 2020 in Laravel by Niroj
• 82,560 points
1,402 views
0 votes
1 answer

How to redirect to login if user is not authenticated in laravel?

Hello @kartik, Use middleware for this purpose and ...READ MORE

answered Oct 28, 2020 in Laravel by Niroj
• 82,560 points
980 views
0 votes
1 answer

How to Check for a Specific Type of Object in PHP?

Hello @kartik, Use: bool is_a ( object $object ...READ MORE

answered Oct 29, 2020 in Laravel by Niroj
• 82,560 points
50 views
0 votes
1 answer

How do you check if a field is not null with Eloquent?

Hello @kartik, Simply,we can use Model::whereNotNull('sent_at'); Or Model::whereRaw('sent_at is not null'); Thank ...READ MORE

answered Nov 11, 2020 in Laravel by Niroj
• 82,560 points
211 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,560 points
5,460 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,560 points
635 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,560 points
487 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,560 points
6,058 views
+1 vote
1 answer

How to check if a record already exists in a laravel?

Hey, As a newbie it is most common ...READ MORE

answered Mar 31, 2020 in Laravel by Niroj
• 82,560 points
9,095 views
0 votes
1 answer

How to check if a cookie is set in laravel?

Hello @kartik, You can change: @if (Cookie::get('cookiename') !== false) to @if ...READ MORE

answered Dec 6, 2020 in Laravel by Niroj
• 82,560 points
158 views