刀片:为foreach()提供无效参数。

问题描述 投票:0回答:2

有时$structures数组的值为空,其他变量的代码也能正常工作,但在这种情况下就不行了。

@foreach($structures ?? [] as $item) {{ $item }} @endforeach

php laravel laravel-blade php-7
2个回答
0
投票

我通过添加 () 左右 $structures ?? [] 的表达方式。

@foreach(($structures ?? []) as $item) {{ $item }} @endforeach


0
投票

使用 forelse 而不是 foreach

@forelse ($structures as $item)
{{ $item }}
@empty 
No Items found.
@endforelse

forelse 检查条件如下。

@if ($structures->count())
  @foreach ($structures as $item)  
 {{ $item }}
  @endforeach
@else
No Items found.
@endif
© www.soinside.com 2019 - 2024. All rights reserved.