How to check if a record already exists in a laravel

+1 vote

Hii everone,

I'm new to Laravel. so please excuse the newbie question but how do I find if a record exists?

$user = User::where('email', '=', Input::get('email'));

What can I do here to see if $user has a record?

Mar 31, 2020 in Laravel by kartik
• 37,510 points
31,060 views

1 answer to this question.

0 votes

Hey,

As a newbie it is most common problem that everyone should face.

If you want to use the user object if it exists:

$user = User::where('email', '=', Input::get('email'))->first();
if ($user === null) {
   // user doesn't exist
}

And if you only want to check

if (User::where('email', '=', Input::get('email'))->count() > 0) {
   // user found
}

Or even nicer

if (User::where('email', '=', Input::get('email'))->exists()) {
   // user found
}

Hope by doing or following the above code your problem is solved!

Thank you!

answered Mar 31, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
1 answer

How to delete a single record in Laravel 5?

Hello @kartik, Delete an existing Model $user = User::find(1); $user->delete(); Deleting ...READ MORE

answered Dec 2, 2020 in Laravel by Niroj
• 82,880 points
1,698 views
0 votes
1 answer

How to check if a user email already exist?

Hello @kartik, The validation feature built into Laravel ...READ MORE

answered Dec 7, 2020 in Laravel by Niroj
• 82,880 points
3,123 views
0 votes
1 answer

How to mock a static facade methods in Laravel?

Hey, Facades provide a "static" interface to classes ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,880 points
5,988 views
0 votes
1 answer

How can we create a record in Laravel using eloquent?

We need to create a new model ...READ MORE

answered Mar 20, 2020 in Laravel by Niroj
• 82,880 points
959 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,880 points
21,862 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,880 points
2,675 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,880 points
2,532 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,880 points
41,686 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 7, 2020 in Laravel by Niroj
• 82,880 points
3,632 views
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 28, 2020 in Laravel by Niroj
• 82,880 points
7,990 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP