Laravel Checking If a Record Exists


I happen to be new at Laravel and PHP. So please excuse the newbie question but how do I find if there are any records that already exist in the database so I don’t duplicate entries?

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

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


It will generally depend on what you want to do with that object after the check.

Here is what to do 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 in case you only want to check and do something else

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

Or even nicer, you can actually use the exists() method, which is more elegant

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

About the author