Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查变量在laravel blade中是否有数据_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 如何检查变量在laravel blade中是否有数据

Php 如何检查变量在laravel blade中是否有数据,php,laravel,laravel-5,Php,Laravel,Laravel 5,我想检查blade中是否存在变量。为此,我使用了以下行: @if(is_null($products)) <div class="alert alert-warning"> <strong>Sorry!</strong> No Product Found. </div> @else @foreach($products as $pr

我想检查blade中是否存在变量。为此,我使用了以下行:

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

    @foreach($products as $product)
        //
    @endforeach
@endif
我通常使用:


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

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

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

检查长度如何

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

@空的 没有产品

@endforelse

您可以像这样检查

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

    @foreach($products as $product)
    //
    @endforeach
@endif
@if(isset($products)&&&!empty($products))
对不起未找到任何产品。
@否则
@foreach($products as$product)
//
@endforeach
@恩迪夫
试试这个

              @forelse($products as $product)
                 //
              @empty
                <div class="alert alert-warning">
                    <strong>Sorry!</strong> No Product Found.
                </div>
              @endforelse
@forelse($products作为$product)
//
@空的
对不起未找到任何产品。
@endforelse

is\u null查找给定变量是否为null。但是在我们的例子中,我们需要检查值是否为空。对于这个问题,您可以使用isset()或empty()函数,这两个函数在您的例子中的工作方式相同

while isset-确定是否设置了变量且该变量不为NULL,以及

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
@if(isset($products)&&&!empty($products))
@foreach($products as$product)
//
@endforeach
@否则
对不起未找到任何产品。
@恩迪夫

您可以在文档中看到:

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

从Laravel 5.7开始,您还可以执行以下操作:

@empty(!$products)
   <div class="alert alert-warning">
        <strong>Sorry!</strong> No Product Found.
   </div>
@endempty
@空(!$products)
对不起未找到任何产品。
@可爱的
这样做, 检查是否有任何记录“->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
@if($products->count()>0)
@foreach($products as$product)
//在这里输入代码
@endforeach
@否则
对不起未找到任何产品。
@恩迪夫

我发现实现您在这里尝试的目标最有效(也是最简单的方法)的方法如下

Assumption #1: You Know The Variable Exists Within The View.

REMEMBER: an empty array will always return false.
Therefore, there is no real need to run it through a function like empty or is null. 
Comparing it to null will tell you if it exists or not.
(你可以通过检查变量是否不等于NULL来绕过这个假设(如果你把这个变量传递给视图,那就有点夸张了,所以在我看来,我会
保持简单愚蠢[KISS]
——如果你愿意的话,你可以在以后进一步重构的时候尽情享受)

无论如何..

我会坚持使用与您现在使用的代码非常相似的代码,可能类似于以下内容的代码将用于您的视图:

@if(!$products)

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

@else

    @foreach($products as $product)

        // {{ $product . “code goes here.” }}

    @endforeach

@endif
您还需要包括产品模型、DB(Laravel)和(通常)请求对象,如下所示:

// Laravel Dependencies 
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
// User Created Model
use App\Product;
希望这会有所帮助!

@if(is_null($products))
替换为
@if(!empty($products))
在检查empty()时不需要检查isset(),因为如果变量不存在,则不会生成警告
@forelse ($products as $product)
    //
@empty
    <div class="alert alert-warning">
        <strong>Sorry!</strong> No Product Found.
    </div>      
@endforelse
@empty(!$products)
   <div class="alert alert-warning">
        <strong>Sorry!</strong> No Product Found.
   </div>
@endempty
@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
Assumption #1: You Know The Variable Exists Within The View.

REMEMBER: an empty array will always return false.
Therefore, there is no real need to run it through a function like empty or is null. 
Comparing it to null will tell you if it exists or not.
@if(!$products)

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

@else

    @foreach($products as $product)

        // {{ $product . “code goes here.” }}

    @endforeach

@endif
public function productSearch(Request $request)
{
    // Easily obtain the name submitted by the form (I assume via the request object
    // dependency injection magic
    $name = $request->name; 

    // I would consider using the DB builder tool, as follows, as there is more docs on it
    // see: https://laravel.com/docs/5.6/queries - this will return a collection (iterable) 
    $products = DB::table(‘products’)
                ->where('name' , 'like', '%'.$name.’%’)
                ->get();

    // simply passing to the view
    return view('cart.product', compact('products'));
}

// Laravel Dependencies 
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
// User Created Model
use App\Product;