Laravel 5.8 Eloquent check if record exists

11,807

Solution 1

Laravel's Query Builder has various aggregate methods, one of which is the exists() method wherein you can check if a record exists or not.

//returns true or false
$checker = Purchase::select('id')->where('id',$request->itemid)->exists();

Here's the documentation link for reference:

  1. From Laravel 5.6 to 7.x
  2. Laravel 8

Solution 2

Or a ternary:

echo (Purchase::select('id')->where('id',$request->itemid)->exists()) ? "Has Data" : "No Data";

Solution 3

You can use

if ($checker ->first()) { } 
if (!$checker ->isEmpty()) { }
if ($checker ->count()) { }
if (count($checker )) { }
if ($checker ->isNotEmpty()) { }

You can try any one of these to check and you will get your desired output.

Solution 4

To determine if any records exist that match your query's constraints, you may use the exists and doesntExist methods. It will return true or false:

$checker = Purchase::where('id',$request->itemid)->exists();

if ($checker){
    echo "Has data";
} else {
    echo "None data";
}

Or, you may do the reverse:

$checker = Purchase::where('id',$request->itemid)->doesntExist();

if ($checker){
    echo "None data";
} else {
    echo "Has data";
}

See the Laravel Documentation: https://laravel.com/docs/6.0/queries#where-exists-clauses

Share:
11,807

Related videos on Youtube

kwestionable
Author by

kwestionable

I am a learning programmer/developer.

Updated on June 04, 2022

Comments

  • kwestionable
    kwestionable almost 2 years

    Trying to check if record exists in Purchase class.

    When returning $checker it returns me null however when I use it inside if else it gives me Has data.

    Here's my code:

    $checker = Purchase::select('id')->where('id',$request->itemid)->get();
    if ($checker === null){
        echo "None data";
    } else {
        echo "Has data";
    }
    
    • online Thomas
      online Thomas over 4 years
      If it equals null that would be the case for no record right? You reversed the if/else
    • kwestionable
      kwestionable over 4 years
      @onlineThomas I edited my post, kindly recheck.
    • Ayaz Ali Shah
      Ayaz Ali Shah over 4 years
      Use ->first() instead of ->get()
    • kwestionable
      kwestionable over 4 years
      It doesn't echo anything now @AyazShah
    • Ayaz Ali Shah
      Ayaz Ali Shah over 4 years
      @kwestionable are you forgetting the semi color after it ?
    • kwestionable
      kwestionable over 4 years
      After ->first()? No @AyazShah
    • Ayaz Ali Shah
      Ayaz Ali Shah over 4 years
      @kwestionable yes you must add ; after it so it will be ->first();
    • apokryfos
      apokryfos over 4 years
      ->get() should not return null. It should return a collection, which might be empty.
    • kwestionable
      kwestionable over 4 years
      @AyazShah yep I did, still nothing.
    • kwestionable
      kwestionable over 4 years
      @apokryfos I don't know the term, but it does return [ ]
    • Ayaz Ali Shah
      Ayaz Ali Shah over 4 years
      @kwestionable try this Purchase::where('id',$request->itemid)->first();
    • ColinMD
      ColinMD over 4 years
      @kwestionable it would be handy to see more code, where is itemid coming from? if you dd($request->itemid); what do you get?
    • CHARITRA SHRESTHA
      CHARITRA SHRESTHA over 4 years
      you can use the code provided in the answer section
    • apokryfos
      apokryfos over 4 years
      It should return a Collection if you're getting a [ ] that's probably just an artefact of what you're using to see the output.
  • MrMoxy
    MrMoxy almost 2 years
    Using count(), I get an error "Parameter must be an array or an object that implements Countable"
  • MrMoxy
    MrMoxy almost 2 years
    This is bad syntax: if ($checker ->isNotEmpty()) { } Use if(!empty($checker) { } instead