防止 foreach 在 blade laravel 中多次循环 html 标记
Prevent foreach from loop html tag multiple time in blade laravel
我正在尝试使用 foreach 循环数据,我想在循环第一个项目后跳过 html 标记。
我试过像下面的代码一样,但是 html 标签 <p>
仍然被循环多次。我想要的是 <p>
标签只循环一次
@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
结果示例:
我想要的是这样的:
Also Available at
- Product 1
- Product 2
但是使用上面的代码结果是这样的:
Also Available at
- Product 1
Also Available at
- Product 2
谢谢
我不确定为什么它不工作,它必须工作并且工作相同的代码,你可以试试 --
@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
或者你可以这样使用
@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
有什么问题吗?把它从循环中取出来。
<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
我正在尝试使用 foreach 循环数据,我想在循环第一个项目后跳过 html 标记。
我试过像下面的代码一样,但是 html 标签 <p>
仍然被循环多次。我想要的是 <p>
标签只循环一次
@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
结果示例:
我想要的是这样的:
Also Available at
- Product 1
- Product 2
但是使用上面的代码结果是这样的:
Also Available at
- Product 1
Also Available at
- Product 2
谢谢
我不确定为什么它不工作,它必须工作并且工作相同的代码,你可以试试 --
@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
或者你可以这样使用
@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
有什么问题吗?把它从循环中取出来。
<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