3

How to find the last item in the foreach loop if Im using @if inside the foreach, I want to find it so I want to remove the dot after the last item, I want these dots to be shown only between items like: hello . Help . I know the way using loop-->last, but it doesnt work becuase the @if conditon shows different last item, which means if we have 100 items but when we use if it will show let say the item 1 and 42 and 63 so the last item here is 63 but when use loop->last will show that last item is 100

Here but what I have is like this: Here.

@foreach($cuisines as $cuisine)

    @if(($cuisine->id)==($scuitem->cuisine_id))
        {{ $cuisine->title }} •
    @endif 

@endforeach
2
  • Need you to use $end = end($cuisines) before loop and match with $end->cuisine_id == $cuisine->id inside loop Commented Sep 27, 2019 at 7:04
  • didnt get what you mean Commented Sep 27, 2019 at 13:33

2 Answers 2

4

You can use $loop->last :

@foreach($cuisines as $cuisine)
       @if(($cuisine->id)==($scuitem->cuisine_id))
          {{ $cuisine->title }} 
         @if(!($loop->last))
            •
         @endif
       @endif 
@endforeach

Edited :

Now Use this code in Controller and pass $str to view

$arr = [];
foreach($cuisines as $cuisine){
  if(($cuisine->id)==($scuitem->cuisine_id)){
    $arr[] = $cuisine->title;
  }
}
$str = implode(' • ',$arr);

Or if you want to code in view than use as below :

@php
  $arr = [];
  foreach($cuisines as $cuisine){
    if(($cuisine->id)==($scuitem->cuisine_id)){
      $arr[] = $cuisine->title;
    }
  }
  $str = implode(' • ',$arr);
@endphp
Sign up to request clarification or add additional context in comments.

3 Comments

I know this way but it doesnt work becuase the @if conditon shows different last item, which means if we have 100 items but when we use if it will show let say the item 1 and 42 and 63 so the last item here is 63 but when use loop->last will show that last item is 100, u got what I mean?
thank you but how to print out the result? I tried to make another loop for $arr by using foreach ($arr as newcouisine) but its the same result
@AshMalek, than you just need to code {{ $str }} where you want to display result
2

Laravel actualy has the $loop variable with information about the current iteration.

For example $loop->last is a boolean indicating if we are on the last iteration.

@foreach($cuisines as $cuisine)
    @if(($cuisine->id)==($scuitem->cuisine_id))
        {{ $cuisine->title }}{{ $loop->last ? '' : ' •' }}
    @endif
@endforeach

2 Comments

I know this way but it doesnt work becuase the @if conditon shows different last item, which means if we have 100 items but when we use if it will show let say the item 1 and 42 and 63 so the last item here is 63 but when use loop->last will show that last item is 100, u got what I mean?
Wouldn't it then be a better idea to move the filtering to the controller? Otherwise you will have too much logic in your views.

Your Answer

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