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

0 votes

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 in Laravel by kartik
• 11,290 points
283 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 by Niroj
• 22,790 points

Related Questions In Laravel

0 votes
1 answer

How to check request is ajax or not in Laravel?

Hello, Laravel allow use of their library method that ...READ MORE

answered Mar 23 in Laravel by Niroj
• 22,790 points
58 views
0 votes
1 answer

.How to turn off CSRF protection for a particular route in Laravel?

Hey, We can add that particular URL or ...READ MORE

answered Mar 24 in Laravel by Niroj
• 22,790 points
86 views
0 votes
1 answer

How to make a constant and use globally in laravel?

Hii, You can create a constants.php page in config folder ...READ MORE

answered Mar 24 in Laravel by Niroj
• 22,790 points
34 views
0 votes
1 answer

How to add a new column to existing table of laravel in a migration?

Hii @kartik, To create a migration, you may ...READ MORE

answered Mar 30 in Laravel by Niroj
• 22,790 points
543 views
0 votes
1 answer

How to make anchor tag with routing using Laravel?

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

answered Mar 18 in Laravel by Niroj
• 22,790 points
434 views
0 votes
1 answer

What is redirection in Laravel?

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

answered Mar 18 in Laravel by Niroj
• 22,790 points
62 views
0 votes
1 answer

How to install Laravel via composer?

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

answered Mar 23 in Laravel by Niroj
• 22,790 points
64 views
0 votes
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 in Laravel by Niroj
• 22,790 points
151 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 in Laravel by Niroj
• 22,790 points
111 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 in Laravel by Niroj
• 22,790 points
39 views