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
Naren
  • 179
  • 2
  • 11
  • 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 – Serghei Leonenco Feb 06 '20 at 04:37

5 Answers5

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
Nipun Tharuksha
  • 2,496
  • 4
  • 17
  • 40
Moshiur
  • 659
  • 6
  • 22
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
Hamelraj
  • 4,676
  • 4
  • 19
  • 42
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
Shadiqur
  • 490
  • 1
  • 5
  • 18
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

Nipun Tharuksha
  • 2,496
  • 4
  • 17
  • 40
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
Masood Khan
  • 587
  • 3
  • 8