Home > database >  I got this error on laravel project- Error Exception Trying to get property 'sales_price'
I got this error on laravel project- Error Exception Trying to get property 'sales_price'

Time:10-27

I get this error form helper.php. I need sale prices of product. but I get this error.And I dont understand when I get this error.

helper.php

public static function getSalesPriceUsingProductIDCode($id, $customerId)
{
    $data = PosCustomerProducts::valid()->where('complete_status',0)->where('customer_id', $customerId)->where('product_id', $id)
    ->select('product_id', 'sales_price')
    ->first()->sales_price;
    return $data;
}

orderSheetExport.php

if(isset($results[$product->id]))
{   
    if (Helper::getSalesPriceUsingProductIDCode($product->id,$results['customer_id'])==0)
    { 
        $excel_dynamic_data_values[$index][] =   $product->whole_sale_price * $results[$product->id];
        $productArray[$rsm_id][$product->id]  = $results[$product->id] * $product->whole_sale_price;
        $singleProductArray[$rsm_id][$product->id] = $productArray[$rsm_id][$product->id];
        
    }else
    {    
        $excel_dynamic_data_values[$index][] = Helper::getSalesPriceUsingProductIDCode($product->id,$results['customer_id']) * $results[$product->id];       
        $productArray[$rsm_id][$product->id]        = $results[$product->id] *  Helper::getSalesPriceUsingProductIDCode( $product->id,$results['customer_id']);
        $singleProductArray[$rsm_id][$product->id] = $productArray[$rsm_id][$product->id];   
    }
}

CodePudding user response:

If you just need the sales_price use value function

public static function getSalesPriceUsingProductIDCode($id, $customerId)
{
    $data = PosCustomerProducts::valid()->where('complete_status',0)->where('customer_id', $customerId)->where('product_id', $id)
    ->select('product_id', 'sales_price')
    ->value('sales_price');
    return $data;
}

Kindly refer value() in Laravel Queries

CodePudding user response:

I solve this problem. I also understand Why I was getting this error. When I use this helper function on a loop. It get some values that were not any sale price data. So it though this error. So, I write if else condition on it.

Helper.php

public static function getSalesPriceUsingProductIDCode($id, $customerId)
{
    $data = PosCustomerProducts::valid()->where('complete_status',0)->where('customer_id', $customerId)->where('product_id', $id)
    ->select('product_id', 'sales_price')
    ->first();
        if ($data) {
            $data = $data->sales_price;
        } else {
            $data = 0;
        }
    return $data;
}
  • Related