Laravel Checking If a Record Exists Laravel Checking If a Record Exists laravel laravel

Laravel Checking If a Record Exists


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}


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


One of the best solution is to use the firstOrNew or firstOrCreate method. The documentation has more details on both.