php 需要检查laravel中的对象是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41269198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:21:44  来源:igfitidea点击:

Need to check if an object is empty in laravel

phplaravelblade

提问by prgrm

I create a view by doing an eloquent query and then pass it over to Blade.

我通过做一个雄辩的查询来创建一个视图,然后将它传递给 Blade。

@if($contacts != null)
//display contacts
@else
You dont have contacts
@endif

However it always assume that $contacts has something even if the query gives me nothing.

然而,它总是假设 $contacts 有一些东西,即使查询没有给我任何东西。

I did dd($contacts)and get:

我做了dd($contacts)并得到:

Collection {#247 ▼
  #items: []
}

How do I check if it is empty?

我如何检查它是否为空?

回答by gsueagle2008

If it is a Eloquent Collection as it appears to be from your example you can use the isEmpty collection helper function;

如果它是 Eloquent 集合,就像您的示例中那样,您可以使用 isEmpty 集合辅助函数;

@if(!$contacts->isEmpty())
//display contacts
@else
You dont have contacts
@endif

Collections Documentation

集合文档

回答by Alexey Mezenin

There are few ways:

有几种方法:

if (!empty($contacts))

if (!contacts->isEmpty())

if (count($contacts) > 0)

if ($contacts->count() > 0)

回答by Michel

Your Eloquent query returns an array of result, so you can use count.

您的 Eloquent 查询返回一个结果数组,因此您可以使用count.

@if(count($contacts) > 0)
//Display contacts
@else
//No contacts
@endif

回答by Md. Abu Taleb

Your $contactsis empty. Bcoz Your query is unable to get data. Once your query unable to get data it's return an empty arrya. So check it

你的$contacts是空的。Bcoz 您的查询无法获取数据。一旦您的查询无法获取数据,它就会返回一个空的 arrya。所以检查一下

    @if($contacts->isEmpty())
    {{ 'Empty' }} 
    @else
   {{ 'you have data' }}
    @endif

回答by Dang Cong Duong

You can use blank($contacts)
Helpers Laravel: blank

你可以使用blank($contacts)
Helpers Laravel:空白

回答by Alemoh Rapheal Baja

if it is inside a controller this will help

如果它在控制器内,这将有所帮助

if(empty($query) {
     //do something
}else{
//do some stuff
}

回答by Alemoh Rapheal Baja

if(count($profiles) > 0){
            return redirect()->action('NameController@name');
        }else{
            return view('user');
        }

this also works fine in a controller

这在控制器中也能正常工作

回答by Francisko Kruz Sosa

You must do the following:

您必须执行以下操作:

in the view of your " ContactController":

在您的“ ContactController”视图中:

public function index()
{
   $contacts = Contact::all();
   return view('page.index', compact('contacts'))
}

later on view " index.blade.php":

稍后查看“ index.blade.php”:

@if (collect($contacts)->isEmpty()) {{-- remember that $contact is your variable --}}
   <p>There is no record available at this time</p>
@else
   @foreach($contacts as $contact)
      {{$contact->name_contact}}
   @endforeach
@endif

Now, I suppose you have a model called Contact

现在,我想你有一个名为Contact的模型