[英]How to check object of a model is empty in laravel?
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.我使用以下代码检查了$persons
是否为空。
if($persons){
var_dump($persons);
}
Actually $persons
is empty.实际上$persons
是空的。 But I am getting result for var_dump
as但我得到的结果是var_dump
object(Illuminate\\Database\\Eloquent\\Collection)#417 (1) { ["items":protected]=> array(0) { } }
How will I check $persons
is empty?我将如何检查$persons
是否为空? Can anyone help?任何人都可以帮忙吗?
You can use the isEmpty method:您可以使用 isEmpty 方法:
http://laravel.com/api/5.0/Illuminate/Support/Collection.html#method_isEmpty http://laravel.com/api/5.0/Illuminate/Support/Collection.html#method_isEmpty
Use the count function使用计数功能
@if (count($persons)) @if (count($persons))
If you have eloquent collection, call the function isEmpty()
like this:如果您有 eloquent 集合,请像这样调用函数isEmpty()
:
$persons->isEmpty();
This return true or false.这返回真或假。 Hope this helps.希望这可以帮助。
尝试这个。
is_null($var)?abort('empty'):abort('filled')
You can use isEmpty() method.您可以使用 isEmpty() 方法。
At the same time you can check it by simply with count() method before once you fetch the data.同时,您可以在获取数据之前简单地使用 count() 方法进行检查。
$count = WysPerson::where('family_id', $id)->count();
if($count==0){
return redirect()->back()->withErrors('Empty Data');
}
$persons = WysPerson::where('family_id', $id)->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.