Trying to get property 'sub_products' of non-object
How to fix this error.
$productExists = Product::on('tenant')->with(['brand'])->with('sub_products')->where('id', '=', $id)->first();
$quantity_check = true;
if(!empty(json_decode($productExists->sub_products,true))){ (the error is coming from this line)
foreach(json_decode($productExists->sub_products,true) as $key=> $subProds){
$newColor[$subProds['color']] = $subProds['color_name'];
if(isset($subProds['quantity']) && $subProds['quantity'] > 0){
$quantity_check = false;
}
}
Well try seeing if it is empty
dd($productExists);
$productExists = Product::on('tenant')->with(['brand'])->with('sub_products')->where('id', '=', $id)->first();
This is the query for productExist
App\Product {#1198 ▼
#table: "product"
+timestamps: false
#connection: "tenant"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:44 [▶]
#original: array:44 [▼
"id" => 4021
"code" => null
"name" => "Foundation"
"model" => "fg5668"
"brand_id" => 15
"category_id" => 53
"on_sale" => "1"
"ean13" => null
"upc" => null
"fcc_id" => null
"imei_no" => null
"serial_no" => null
"tax_perc" => "3.00"
"quantity" => 230
"total_sold" => 0
"price" => "7000.00"
"product_description" => null
"wholesale_price" => "6000.00"
"selling_price" => "5000.00"
"discount_amount" => "0.00"
"location" => null
"color" => "{"#e3d4bb":"ivory","#ffa100":"oyeb"}"
"sku" => "[null,null]"
"width" => "0.00"
"width_unit" => "0"
"height" => "0.00"
"height_unit" => "0"
"depth" => "0.00"
"depth_unit" => "0"
"weight" => "0.00"
"weight_unit" => "0"
"scan_serial_no" => "0"
"scan_imei_no" => "0"
"out_of_stock" => "0"
"active" => "1"
"available_date" => "2020-09-08"
"expiry_date" => "2021-09-17"
"is_deleted" => 0
"img_position" => null
"created_on" => "2020-09-22 22:01:06"
"created_by" => 1
"updated_on" => "2020-09-22 22:01:06"
"updated_by" => 40760
"top_seller_status" => "1"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:2 [▼
"brand" => App\ProductBrand {#1113 ▼
#table: "product_brand"
#connection: "tenant"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:11 [▶]
#original: array:11 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
"sub_products" => Illuminate\Database\Eloquent\Collection {#1209 ▶}
]
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
This is what I get when I dd it
Please or to participate in this conversation.