在@foreach中使用laravel {{}}

有人可以向我解释为什么这种语法不起作用吗?

@foreach({{ ($arrayOfProducts->count()/3 == 0) ? '$arrayOfProducts as $item' : '$arrayOfProducts->chunk(($arrayOfProducts->count()/3)) as $chunk' }})

@endforeach

A picture of the code

评论
hquae
hquae

If you look at the [documentation] you need to use the @foreach / @endforeach in a similar way as the standard php foreach loop.

在代码中,实际上是将字符串作为循环的参数传递。如果要使其工作,则需要在项目数组上传递条件,然后对其进行循环。

@foreach(($arrayOfProducts->count()/3 == 0) ? $arrayOfProducts : '$arrayOfProducts->chunk(($arrayOfProducts->count()/3))) as $item)

{{ dump($item) }}

@endforeach
点赞
评论
bjstry
bjstry

您需要学习基本语法。

@foreach($arrayOfProducts as $product)
  {{ $product->someAttr }}
@endforeach

This would work just fine, behind the scenes it's just basic PHP foreach loop, and {{ }} is just echo, also try to keep that conditional logic in your controller for more readable code.

点赞
评论