Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Prevent foreach from loop html tag multiple time in blade laravel

I’m trying to loop data using foreach and I want to skip the html tag after the first item being looped.

I’ve tried like the code bellow, but the html tag <p> still being looped multiple time. What I want is the <p> tags only looped once

@foreach ($store_icon as $key => $icon)
   @if ($key < 1)
       <p class="available-at">Also available at:</p>
   @endif
   <a href="{{ $store->product_url }}" target="_blank" style="margin-right: 2px"></a>
@endforeach

Result example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What I want is like this:

Also Available at
- Product 1
- Product 2

But using the code above it resulted like this:

Also Available at
- Product 1
Also Available at
- Product 2

Thanks

>Solution :

I am not sure why it is not working, It must work and working the same code for as well, you can try —

@foreach ($store_icon as $key => $icon)
   @if ($key == 0)
       <p class="available-at">Also available at:</p>
   @endif
   <a href="{{ $store->product_url }}" target="_blank" style="margin-right: 2px"></a>
@endforeach

OR you can use like this

@if(!empty($store_icon))
<p class="available-at">Also available at:</p>
    @foreach ($store_icon as $key => $icon)
       <a href="{{ $store->product_url }}" target="_blank" style="margin-right: 2px"></a>
    @endforeach
@endif
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading