Laravel Checking If a Record Exists

Ben picture Ben · Nov 23, 2014 · Viewed 488.8k times · Source

I am new to Laravel. 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?

Answer

lukasgeiter picture lukasgeiter · Nov 23, 2014

It depends if you want to work with the user afterwards or only check if one exists.

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
}