【发布时间】:2022-01-19 11:15:53
【问题描述】:
我正在循环浏览具有我所有可配置选项的产品。在里面我正在循环浏览我的项目,当我在产品中找到匹配项时,我想显示这个产品和项目。
为此,我想创建一个名为 $currentProduct 的变量。
@foreach($order['products'] as $product) // Configurable products
@foreach($order['items'] as $item) // Simple products
@if ($item['product_type'] == 'simple')
@if ($product['id'] === $item['product_id'])
@php($currentProduct = $product) // error here $product undefined
@php($currentItem = $item) // error here $item undefined
@endif
@elseif ($item['product_type'] == 'configurable')
@if ($product['id'] == $item['parent_item']['product_id'])
@php($currentProduct = $product) // error here $product undefined
@php($currentItem = $item) // error here $item undefined
@endif
@endif
@endforeach
@endforeach
我是 laravel 和刀片模板的新手,我似乎无法理解,为什么 $product 和 $item 在同一个模板中在上面定义时是未定义的。
任何帮助表示赞赏
【问题讨论】:
-
我对 Laravel Blade 几乎没有经验,但我看到的所有示例都包含在
@php和@endphp之间分配变量的代码。你可以试试吗? -
给出同样的错误。我不认为这是一个语法问题,因为 @php() 被接受了。我可以定义其他变量,我只是无法到达@php 标签之外的变量
标签: php laravel laravel-blade