Search code examples
phplaravelcontroller

Attempt to read property "productImages" on null


                            <tbody>
                                @if ($items->isNotEmpty())
                                    @foreach ($items as $item)
                                        @php
                                            $product = $item->associatedModel; 
                                            $image = !empty($product->productImages->first()) ? asset('storage') . '/' . $product->productImages->first()->path : asset('themes/ezone/assets/img/cart/3.jpg');
                                        @endphp
                                        <tr>
                                            <td class="product-remove">
                                                <a href="{{ route('carts.remove', ['id' => $item->id]) }}" class="delete"><i class="pe-7s-close"></i></a>
                                            </td>
                                            <td class="product-thumbnail">
                                                <a href="{{ route('product.show', ['slug' => $product->slug]) }}"><img src="{{ $image }}" alt="{{ $product->name }}" style="width:100px"></a>
                                            </td>
                                            <td class="product-name"><a href="{{ route('product.show', ['slug' => $product->slug]) }}">{{ $product->name }}</a></td>
                                            <td class="product-price-cart"><span class="amount">{{ number_format($item->price) }}</span></td>
                                            <td class="product-quantity">
                                                <input type="number" name="quantity" value="{{ $item->quantity }}" min="1" required class="item-quantity" data-item-id="{{ $item->id }}">
                                            </td>
                                            <td class="product-subtotal">
                                                <span id="subtotal-{{ $item->id }}">{{ number_format($item->price * $item->quantity) }}</span>
                                            </td>
                                        </tr>
                                    @endforeach
                                @else
                                    <tr>
                                        <td colspan="6">The cart is empty!</td>
                                    </tr>
                                @endif
                            </tbody>

Can anyone help me, i dont know where where the wrong code in my view . But I think I have changed a lot of code but it is still the same?


Solution

  • Check product is exist before access its attributes

    @php
        $product = $item->associatedModel;
        if ($product) {
            $image = !empty($product->productImages->first()) ? asset('storage') . '/' . $product->productImages->first()->path : asset('themes/ezone/assets/img/cart/3.jpg');
        } else {
            # if product is null
            $image = asset('themes/ezone/assets/img/cart/3.jpg');
        }
    @endphp