[英]@php tag in blade template can't reach inline variable
I am looping through products which has all my configurable options available.我正在循环浏览具有我所有可配置选项的产品。 Inside that i am looping through my items, when i get a match in product i want to display this product and item.在里面我正在循环浏览我的项目,当我在产品中找到匹配项时,我想显示这个产品和项目。
To do so i want to create a variable called $currentProduct.为此,我想创建一个名为 $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
I am new to laravel and blade templates and can't seem to wrap my head around, why $product and $item is undefined when they are defined right above in same template.我是 laravel 和刀片模板的新手,我似乎无法理解,为什么 $product 和 $item 在同一个模板的正上方定义时未定义。
Any help appreciated任何帮助表示赞赏
You put the code between @php
and @endphp
but without parantheses:您将代码放在@php
和@endphp
之间,但没有括号:
@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;
$currentItem = $item;
@endphp
@endif
@elseif ($item['product_type'] == 'configurable')
@if ($product['id'] == $item['parent_item']['product_id'])
@php
$currentProduct = $product;
$currentItem = $item;
@endphp
@endif
@endif
@endforeach
@endforeach
The variables can then be used below where they were defined.然后可以在定义变量的地方使用这些变量。
See documentation about raw PHP in blade templates here:请参阅此处刀片模板中有关原始 PHP 的文档:
https://laravel.com/docs/8.x/blade#raw-php https://laravel.com/docs/8.x/blade#raw-php
any @php must close with @endphp任何@php 都必须以@endphp 结束
Would you please try the below code?你能试试下面的代码吗? ` `
@foreach($order['products'] as $product)
@foreach($order['items'] as $item)
@if ($item['product_type'] == 'simple')
@if ($product['id'] === $item['product_id'])
@php ($currentProduct = $product) @endphp
@php ($currentItem = $item) @endphp
@endif
@elseif ($item['product_type'] == 'configurable')
@if ($product['id'] == $item['parent_item']['product_id'])
@php ($currentProduct = $product) @endphp
@php ($currentItem = $item) @endphp
@endif
@endif
@endforeach
@endforeach
` `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.