我正在瀏覽具有我所有可配置選項的產品。在里面我回圈瀏覽我的物品,當我在產品中找到匹配時,我想展示這個產品和物品。
為此,我想創建一個名為 $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 和 Blade 模板的新手,似乎無法理解,為什么 $product 和 $item 在同一模板的正上方定義時未定義。
任何幫助表示贊賞
uj5u.com熱心網友回復:
您將代碼放在和之間@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
uj5u.com熱心網友回復:
任何@php 必須用@endphp 關閉
uj5u.com熱心網友回復:
你可以試試下面的代碼嗎?`
@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
`
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/384627.html
標籤:php 拉拉维尔 laravel-blade
下一篇:以角度保護laravelapi
