9

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 $products on blade I could show inside of foreach loop but when i get empty variable.I couldn't show the message No Data Found instead it shows only empty space? is there any problem of checking variable inside of blade?

Controller code :

public function productSearch(Request $request)
    {
        $name = $request->name; 
        $products = Product::where('name' , 'like', '%'.$name.'%')->get();
        return view('cart.product',compact('products'));
    }
1
  • replace @if(is_null($products)) with @if(!empty($products)) Commented May 4, 2017 at 5:22

11 Answers 11

9

I generally use PHP count() :

@if(count($products) < 1)
    <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 :

 @if(!empty($products))
Sign up to request clarification or add additional context in comments.

1 Comment

PHP empty() is super bad practice... do NOT use it FGS. Ez: @notEmpty($products)
6

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 $users variables is empty, then it will display a paragraph

so for you :

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

Comments

6

As of Laravel 5.7, You can also do this:

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

1 Comment

@YoushaAleayoub @notEmpty is not a default Laravel directive
4

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

1 Comment

You dont need to check isset() when checking empty() as no warning is generated if the variable does not exist
2

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

Comments

2

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

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

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

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

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

Comments

2

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

@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

Comments

1

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

Comments

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

Refer

Comments

1

Try this

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

Comments

0

I found the most effective (and by far the easiest way) of accomplishing what you're trying here to be as follows.

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.

(You could by-pass this assumption by checking to see if the variable is not equal to NULL (it's kind of bloaty if you've passed that variable through to the view, so in my opinion, I would KEEP IT SIMPLE STUPID [KISS] - if you want, you can go all fancy later when it comes to further refactoring).

ANYWAY..

I would stick to pretty similar code as you have now, maybe something like this here would be the code for your view:

@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

and the code for your controller would look something like this (you almost had it, remember: "perfect practice makes perfect!" - but yeah, the controller code:

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'));
}

You would also need to include the Product model, DB (Laravel) and (as per usual) the request object, as follows:

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

Hopefully, this has been helpful!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.