2

else condition, sorry no items in your cart not printing when the cart is empty

@if($datas)
   @foreach($datas as $data)
   <h5>This is product</h5>
    @endforeach
@else
        <h5>Sorry no items in your cart</h5>    // Not printing when cart is empty  
@endif
1
  • 1
    i guess you receive an array of objects in datas, right? have you tried to count it? and can you var_dump the initial entry of the datas before the if statement Commented Feb 6, 2020 at 4:37

5 Answers 5

2

This might be you are getting $datas as an empty collection.

If you are getting a collection in $datas from Eloquent model then you can check it with isNotEmpty as below

@if($datas->isNotEmpty())
    @foreach($datas as $data)
        <h5>This is product</h5>
    @endforeach
@else
    // $data is empty
@endif

for more see documentation

or you can simply check it by empty() (for arrays) method as below

@if(!empty($datas))
    @foreach($datas as $data)
        <h5>This is product</h5>
    @endforeach
@else
    // $data is empty
@endif
Sign up to request clarification or add additional context in comments.

Comments

1

if its array data then use empty()

@if(!empty($datas))
@else
@endif

or other way use count()

@if(count($datas) > 0)
@else
@endif

or if its collection then use isEmpty()

@if(!$datas->isEmpty())
@else
@endif

Comments

1

check the array is empty or not with empty() function

@if(!empty($datas))
    @foreach($datas as $data)
        <h5>This is product</h5>
    @endforeach
@else
    //do 
@endif

Comments

0

I can see answers almost. But let me explain bit more.I can see that you are checking $datas is empty or not. If you use helper function dd() to dump $datas values you can see that it always return a Collection from Illuminate\Support\Collection. So even $datas is empty it gives a empty Collection. You can test this by your self like below in your controller

if($datas) {
   dd($datas);
}else{
   dd('empty');
}

This will always show the empty Collection. So just using if condition you cann't check a collection is empty. Instead of if you can check a collection with below methods.

  1. collect([])->isEmpty();

    @if ($datas->isEmpty())
    
    @endif
    
  2. collect([])->isNotEmpty();

    @if ($datas->isNotEmpty())
    
    @endif
    
  3. collect([])->first();

    @if ($datas->first())
    
    @endif
    
  4. collect([])->count();

    @if ($datas->count())
    
    @endif
    

If you check Laravel Collection documentation you can find more.

Collections full documentation

Location above methods available

Edit 01 This answer directly answer your question. Please check

Eloquent Collection: Counting and Detect Empty

Comments

0

You can use laravel's forelse loop - https://laravel.com/docs/6.x/blade#loops

@forelse ($datas as $data)
    <h5>This is product</h5>
@empty
    <h5>Sorry no items in your cart</h5>
@endforelse

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.