Laravel Checking If a Record Exists

660,057

Solution 1

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
}

Solution 2

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

Solution 3

In laravel eloquent, has default exists() method, refer followed example.

if (User::where('id', $user_id )->exists()) {
    // your code...
}

Solution 4

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

Solution 5

if($user->isEmpty()){
    // has no records
}

Eloquent uses collections. See the following link: https://laravel.com/docs/5.4/eloquent-collections

Share:
660,057
Ben
Author by

Ben

Updated on July 08, 2022

Comments

  • Ben
    Ben almost 2 years

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

  • Volatil3
    Volatil3 over 8 years
    if you call exists() against a non existent record then it gives error: Call to a member function exists() on null
  • lukasgeiter
    lukasgeiter over 8 years
    @Volatil3 You are doing something wrong. You can't call exists after you've already ran the query
  • Volatil3
    Volatil3 over 8 years
    @lukasgeiter guess you are right. I had already called first()
  • Combine
    Combine over 8 years
    I used to use -> $email_exists = DB::table('users')->select('email')->where('email', $request->email)->count(); but your method is better :)
  • jshbrmn
    jshbrmn over 8 years
    alternatively, if (!User::where('email', '=', Input::get('email'))->exists())... could work
  • Janaka Pushpakumara
    Janaka Pushpakumara about 8 years
    I think this is the better way to find if User is exists. User::where('email', '=', 'value')->count() > 0;
  • Gokigooooks
    Gokigooooks about 8 years
    while not fitting the question, still, very useful functions. the difference between the two is that firstOrNew instantiates an instance of the model called while firstOrCreate saves the queried model instantly so you need to update changes on the firstOrCreate'd model.
  • TrueStory
    TrueStory about 7 years
    If you have a table User with ...say over1,000,000,000 records, you'll be checking for a veeeeeeery long time
  • Pathros
    Pathros over 6 years
    @JanakaPushpakumara That alone has sometimes caused me error. I also add && isset()
  • Agil
    Agil over 6 years
    exists() might be a good option too but I feel like it is not the most optimised way, is it?
  • Pezhvak
    Pezhvak over 6 years
    @Volatil3 i just tested ->exists() with laravel 5.5, it says false if it doesn't exist.
  • Roark
    Roark about 6 years
    FYI, you can leave out the '=' value: User::where('email', Input::get('email'))->exists();
  • user3574492
    user3574492 almost 6 years
    Yeah but its not returning a collection. It's returning a single model object as you would assume each user has a unique email so ->isEmpty() will throw an error.
  • Pathros
    Pathros over 5 years
    And what if I get the following error: Call to a member function isEmpty() on null
  • JustAMartin
    JustAMartin over 5 years
    Why making SQL to continue counting rows with count after it has already found one? Exists is more appropriate here - it stops right after finding one match. Even if performance difference might be negligible, still it expresses the intention better.
  • umarbilal
    umarbilal over 5 years
    What if you want to check for multiple row exist?
  • Edgar Ortega
    Edgar Ortega about 5 years
    You could replace find with where. User::where(id, 1)->first()
  • William Turrell
    William Turrell almost 5 years
    Yep, or another way of thinking of it is, use firstOrCreate if you can pass all the attributes in one go (use the second parameter), but firstOrNew if you're going to need further logic before saving.
  • Peter Wolf
    Peter Wolf almost 5 years
  • Robo Robok
    Robo Robok over 4 years
    This should be the accepted answer. The most efficient and dedicated way to do it is through the exists() method.
  • BonisTech
    BonisTech over 3 years
    @Pezhvak Same here with laravel 7.x
  • AliN11
    AliN11 over 3 years
    Note that by using count, you are counting a certain record throughout the table. While the exists method returns true if the table contains at least one desired record.
  • AliN11
    AliN11 over 3 years
    Please consider that count method counts records and it is not good performance-wise in this scenario.
  • AliN11
    AliN11 over 3 years
    Fetching all records, then counting?!
  • simpson
    simpson over 2 years
    in laravel 8.x ;exists() returns false if no row found.so it is safe to use.
  • Inderjeet
    Inderjeet over 2 years
    Not working with milliseconds case. Is there any way to fix this?
  • Hamid Teimouri
    Hamid Teimouri over 2 years
    @AliN11 I did not use count method