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:
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
Related videos on Youtube
Comments
-
kwestionable almost 2 years
Trying to check if
record
exists inPurchase
class.When returning
$checker
it returns menull
however when I use it insideif else
it gives meHas 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 over 4 yearsIf it equals null that would be the case for no record right? You reversed the if/else
-
kwestionable over 4 years@onlineThomas I edited my post, kindly recheck.
-
Ayaz Ali Shah over 4 yearsUse
->first()
instead of->get()
-
kwestionable over 4 yearsIt doesn't echo anything now @AyazShah
-
Ayaz Ali Shah over 4 years@kwestionable are you forgetting the semi color after it ?
-
kwestionable over 4 yearsAfter
->first()
? No @AyazShah -
Ayaz Ali Shah over 4 years@kwestionable yes you must add
;
after it so it will be->first();
-
apokryfos over 4 years
->get()
should not return null. It should return a collection, which might be empty. -
kwestionable over 4 years@AyazShah yep I did, still nothing.
-
kwestionable over 4 years@apokryfos I don't know the term, but it does return
[ ]
-
Ayaz Ali Shah over 4 years@kwestionable try this
Purchase::where('id',$request->itemid)->first();
-
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 over 4 yearsyou can use the code provided in the answer section
-
apokryfos over 4 yearsIt 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 almost 2 yearsUsing count(), I get an error "Parameter must be an array or an object that implements Countable"
-
MrMoxy almost 2 yearsThis is bad syntax: if ($checker ->isNotEmpty()) { } Use if(!empty($checker) { } instead