Level 56
1 - can you show the result of dd($subcategory)?
2 - Does the SubCategory model have a relation method defined with ProductCategory?
3 - Can you show the migration for the ProductCategory model?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
My controller
$subcategory = SubCategory::find($id);
$cat = ProductCategory::find($subcategory->category_id);
dd($cat);
I am getting the value for ````subcategory, but my $catis giving menull```;
Add, if not exists, to the SubCategory model:
public function productCategory(){
return $this->belongsTo(ProductCategory::class, 'category_id', 'id');
}
And then try running this:
$subcategory = SubCategory::with(['productCategory'])->find($id);
$cat = $subcategory->productCategory;
dd($subcategory, $cat);
Please or to participate in this conversation.