php 如何检查变量在laravel刀片中是否有数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43774129/
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:54:08  来源:igfitidea点击:

How to check if a variable has data in laravel blade

phplaravellaravel-5

提问by User57

I wanted to check if variable is there or not in blade ..for that i have used following lines:

我想检查刀片中是否存在变量..为此,我使用了以下几行:

@if(is_null($products))
    <div class="alert alert-warning">
        <strong>Sorry!</strong> No Product Found.
    </div>                                      
@else

    @foreach($products as $product)
        //
    @endforeach
@endif

The problem is when there is $productson blade I could show inside of foreachloop but when i get empty variable.I couldn't show the message No Data Foundinstead it shows only empty space? is there any problem of checking variable inside of blade?

问题是当$products刀片上有时,我可以在foreach循环内显示,但是当我得到空变量时。我无法显示消息,No Data Found而是只显示空白空间?检查刀片内部的变量有什么问题吗?

Controller code :

控制器代码:

public function productSearch(Request $request)
    {
        $name = $request->name; 
        $products = Product::where('name' , 'like', '%'.$name.'%')->get();
        return view('cart.product',compact('products'));
    }

回答by Sanchit Gupta

I generally use PHP count():

我通常使用PHP count()

@if(count($products) > 0)
    <div class="alert alert-warning">
        <strong>Sorry!</strong> No Product Found.
    </div>                                      
@else

    @foreach($products as $product)
        //
    @endforeach
@endif

You may also check with PHP empty()like :

您还可以使用PHP empty() 进行检查,例如:

 @if(!empty($products) > 0)

回答by marmeladze

What about checking length?

检查长度呢?

@if(count($products)) >= 1)
    @foreach($products as $product)
        //
    @endforeach
@else
    <div class="alert alert-warning">
        <strong>Sorry!</strong> No Product Found.
    </div>                                      
@endif

Because empty set (i mean a data stucture with zero elements) is not null at all.

因为空集(我的意思是具有零元素的数据结构)根本不为空。

php > $a = [];
php > echo is_null($a) ? 1 : 0;
// => 0

回答by Mathieu Ferre

As you can see in the documentation :

正如您在文档中看到的:

@forelse ($users as $user)
    <li>{{ $user->name }}</li>
@empty
    <p>No users</p>
@endforelse

This code will allow you to parse all the users and display a list of them. if the $usersvariables is empty, then it will display a paragraph

此代码将允许您解析所有用户并显示他们的列表。如果$users变量为空,则显示一个段落

so for you :

所以对你来说:

@forelse ($products as $product)
    //
@empty
    <div class="alert alert-warning">
        <strong>Sorry!</strong> No Product Found.
    </div>      
@endforelse

回答by Shreyansh Panchal

As of Laravel 5.7, You can also do this:

从 Laravel 5.7 开始,您还可以这样做:

@empty(!$products)
   <div class="alert alert-warning">
        <strong>Sorry!</strong> No Product Found.
   </div>
@endempty

回答by Ervin Ahmed Cajic

Do this, Check is there any records "->count() > 0" then do foreach, else alert.

这样做,检查是否有任何记录“->count()> 0”然后执行foreach,否则警报。

@if ($products->count() > 0 )
    @foreach($products as $product)
        //enter code here
    @endforeach
@else
   <div class="alert alert-warning">
        <strong>Sorry!</strong> No Product Found.
    </div>
@endif

回答by siva

@forelse($products as $product)
 <p>do some thing</p>
@empty
 <p>No Products</p>
@endforelse

Refer

参考

回答by Meera Tank

You can check like

你可以检查像

@if(isset($products) && !empty($products))
<div class="alert alert-warning">
    <strong>Sorry!</strong> No Product Found.
</div>                                      
@else

    @foreach($products as $product)
    //
    @endforeach
@endif

回答by Dharmesh Rakholia

Try this

尝试这个

              @forelse($products as $product)
                 //
              @empty
                <div class="alert alert-warning">
                    <strong>Sorry!</strong> No Product Found.
                </div>
              @endforelse

回答by Gaurav Gupta

is_null Finds whether the given variable is NULL or not. but in our case we need to check whether the value in empty or not for this you can use either isset() or empty() function both work same in your case

is_null 查找给定变量是否为 NULL。但在我们的例子中,我们需要检查该值是否为空,您可以使用 isset() 或 empty() 函数在您的情况下工作相同

while isset — Determine if a variable is set and is not NULL and

while isset — 确定变量是否已设置且不为 NULL 并且

empty — Determine whether a variable is empty and also tell variable is set

empty — 判断一个变量是否为空,并告诉变量是否已设置

@if(isset($products)  && !empty($products))
        @foreach($products as $product)
        //
    @endforeach                                  
@else

    <div class="alert alert-warning">
        <strong>Sorry!</strong> No Product Found.
    </div>
@endif

回答by Kristoff

For me I will use logic like this

对我来说,我会使用这样的逻辑

if(!$products->isEmpty()){
      return view('cart.product', compact('products'));
 }else{
   return view('pageerror', compact('products'));
 }

then you can call pageerror from your view folder to display any page that does not has data

然后你可以从你的视图文件夹中调用 pageerror 来显示任何没有数据的页面