Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephen waweru's avatar

Trying to access array offset on value of type null in laravel 8

In my eCommerce project, I have products with attributes and products that don't have attributes. I have been able to create a function whereby both are added to the cart and also I want them to be able to display on the cart page. I have created 2 functions in the product model that shows the product attributes only which processes the discounted prices for both types of product, with attributes and those without. when I add products with attributes to the cart the page shows the products in the cart page but when I add the products without products I get an error Trying to access array offset on the value of type null.I have traced where the error is coming from and it's coming from this line

$final_price=$proattrprice['productattr_price']-($proattrprice['productattr_price']*$prodetails['product_discount']/100);

In the getdiscountedattrprice function in the product model. I haven't understood why it's calling this function yet I have put a check in the blade file. i have dumped $proattrprice['productattr_price'] and I get the value is null.am not understand why am getting this error

cart blade file

<tbody>
    <?php $total_price=0; ?>
     @forelse ($cartitems as $item)
      <?php$attrpric=Merchadise::getdiscountedattrprice($item['product_id'],$item['size']);
        ?>,
            <tr>
                <td>{{ $item->product->merch_name }}</td>
                @if ($item->product->is_attribute==1)
                    <td>{{ $attrpric['merch_price'] }}</td>
                @else
                    <td>{{ $item->product->merch_price }}</td>
                @endif
                
                <td>
                    <img src="{{ asset ('images/productimages/small/'.$item->product->merch_image) }}" style="width:100px; height:100px;" alt="Product">
                </td>
                <td>
                    <button class="itemupdate qtyminus" type="button" data-cartid="{{ $item->id }}">
                        <i class="fa fa-minus" aria-hidden="true"></i>
                    </button>
                    <input data-id={{ $item->id }} class="quantity" min="1" name="quantity[]" value="{{ $item->quantity }}" type="number">
                    <button class="itemupdate qtyplus" type="button" data-cartid="{{ $item->id }}">
                        <i class="fa fa-plus" aria-hidden="true"></i>
                    </button>
                </td>
                @if ($item->product->is_attribute==1)
                    <td>sh.{{ $attrpric['discount'] * $item['quantity'] }}</td>
                    <td>sh.{{ $attrpric['final_price'] * $item['quantity'] }}</td>
                @elseif($item->product->is_attribute==0)
                    <?php $discountedprice=Merchadise::getdiscountedprice($item['product_id']);
                    ?>
                    <td>{{ ($item->product->merch_price-$discountedprice) * $item['quantity'] }}</td>
                    <td>{{ $discountedprice * $item['quantity'] }}</td>
                @endif
                
                <td>
                    <form method="POST" action="{{ route('deletecartitem', $item->id) }}">
                        @csrf
                        <input name="method" type="hidden" value="DELETE">
                        <button type="submit" class="btn btn-xs btn-danger btn-flat show_confirm" data-toggle="tooltip" title='Delete'>Delete</button>
                    </form>
                </td>
            </tr>
        @if ($item->product->is_attribute==1)
            <?php $attributetotal_price=$total_price+($attrpric['final_price'] * $item['quantity'] );?>
        @elseif($item->product->is_attribute==0)
            <?php $noattributetotal_price=$total_price+($discountedprice * $item['quantity'] );?>
        @endif
        
        @empty
            <P style="font-size: 15px; text-align:center;margin:40px;">Your Cart is Empty.Click <a href="{{url('products')}}">here</a> to shop for products</p>
        @endforelse
    </tbody>

product model. i have named it the merchandise model

public static function getdiscountedprice($product_id){
        $prodetails=Merchadise::select('merch_price','product_discount','merchcat_id')
            ->where('id',$product_id)->first();

        $catdetails=Merchadisecategory::select('category_discount')
            // ->where('id',$prodetails['category_id'])
            ->first();

        if($prodetails['product_discount']>0){
            $discountedprice=$prodetails['merch_price']-($prodetails['merch_price']*$prodetails['product_discount']/100);

        }
        elseif($catdetails['category_discount']>0){
            $discountedprice=$prodetails['merch_price']-($prodetails['merch_price']*$catdetails['category_discount']/100);
        }
        else
        {
            $discountedprice=0;
        }
        return $discountedprice;
    }

    public static function getdiscountedattrprice($product_id,$productattr_size){
        $proattrprice=Productattribute::
        select('product_id','productattr_size','productattr_price')
        ->
        where(['product_id'=>$product_id,'productattr_size'=>$productattr_size])
        // ->
        ->first();

        $prodetails=Merchadise::select('merch_price','product_discount','merchcat_id')
            ->where('id',$product_id)->first();

        $catdetails=Merchadisecategory::select('category_discount')->where('id',$prodetails['merchcat_id'])->first();

        if($prodetails['product_discount']>0){
            
            $final_price=$proattrprice['productattr_price']-($proattrprice['productattr_price']*$prodetails['product_discount']/100);
            
            $discount=$proattrprice['productattr_price']-$final_price;     
            
            
        }elseif($catdetails['category_discount']>0){
            $final_price=$proattrprice['productattr_price']-($proattrprice['productattr_price']*$catdetails['category_discount']/100);
            $discount=$proattrprice['productattr_price']-$final_price;
        }else{
            $final_price=$proattrprice['productattr_price'];
            $discount=0;

        } 

        return array ('merch_price'=>$proattrprice['productattr_price'],
        'final_price'=>$final_price,
        'discount'=>$discount);
    }

here is all my code.where might i be going wrong in my code

0 likes
7 replies
Snapey's avatar

have you heard of n+1 issues? Why are you doing database queries inside a loop in the view?

prepare your data before sending it to the view

1 like
tykus's avatar

@stephen waweru both of these queries will produce null if there is no matching record in the database

$proattrprice=Productattribute::select('product_id','productattr_size','productattr_price')
    ->where(['product_id' => $product_id,'productattr_size' => $productattr_size])
    ->first();

$prodetails=Merchadise::select('merch_price','product_discount','merchcat_id')
    ->where('id', $product_id)
    ->first();

Check that you actually get a value before proceeding

if (!$proattrprice) {
   // no record, return early
}
1 like
stephen waweru's avatar

@tykus but already i have a value in the carts table in database. please explain to me how the error might be coming through

tykus's avatar

@stephen waweru

I have traced where the error is coming from and it's coming from this line $final_price = $proattrprice['productattr_price'] - ($proattrprice['productattr_price'] * $prodetails['product_discount']/100);

It seems that there is not a matching record in the database 🤷‍♂️

Aside, you are allowed to use spaces in your code 😉

1 like
stephen waweru's avatar
stephen waweru
OP
Best Answer
Level 2

this is how i was able to solve the error i added an check in the blade file for each of the variables for each of the prouct type if($item->product->is_attribute==0)

endif

if($item->product->is_attribute==1)

endif

1 like

Please or to participate in this conversation.