Home > Mobile >  @php tag in blade template can't reach inline variable
@php tag in blade template can't reach inline variable

Time:12-16

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.

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

Any help appreciated

CodePudding user response:

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

`

CodePudding user response:

any @php must close with @endphp

  • Related