Reputation: 1735
I am accessing my database using model by using following code.
$persons = WysPerson::where('family_id', $id)->get();
I checked $persons
is empty or not by using following code.
if($persons){
var_dump($persons);
}
Actually $persons
is empty. But I am getting result for var_dump
as
object(Illuminate\Database\Eloquent\Collection)#417 (1) { ["items":protected]=> array(0) { } }
How will I check $persons
is empty? Can anyone help?
Upvotes: 13
Views: 33238
Reputation: 1217
You can use isEmpty() method.
At the same time you can check it by simply with count() method before once you fetch the data.
$count = WysPerson::where('family_id', $id)->count();
if($count==0){
return redirect()->back()->withErrors('Empty Data');
}
$persons = WysPerson::where('family_id', $id)->get();
Upvotes: 0
Reputation: 379
If you have eloquent collection, call the function isEmpty()
like this:
$persons->isEmpty();
This return true or false. Hope this helps.
Upvotes: 4
Reputation: 694
You can use the isEmpty method:
http://laravel.com/api/5.0/Illuminate/Support/Collection.html#method_isEmpty
Upvotes: 16