繁体   English   中英

刀片模板中的@php 标记无法访问内联变量

[英]@php tag in blade template can't reach inline variable

我正在循环浏览具有我所有可配置选项的产品。 在里面我正在循环浏览我的项目,当我在产品中找到匹配项时,我想显示这个产品和项目。

为此,我想创建一个名为 $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 在同一个模板的正上方定义时未定义。

任何帮助表示赞赏

您将代码放在@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

然后可以在定义变量的地方使用这些变量。

请参阅此处刀片模板中有关原始 PHP 的文档:
https://laravel.com/docs/8.x/blade#raw-php

任何@php 都必须以@endphp 结束

你能试试下面的代码吗? `

@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM