I tried sum nested related column but get empty result.
Code:
$data = CropType::with(['categories' => function($cq) {
return $cq->with(['varieties' => function($vq) {
return $vq->with(['products' => function($pq) {
return $pq->sum('total');
}]);
}]);
}])->get();
As you can see, I have 4 tables that are interconnected with foreign keys
Table: crop_types
idtitleTable: categories
idtitlecrop_type_idTable: varieties
idtitlecategory_idTable: products
idtitletotalvariety_id
But when I run my query it's run as like this instead summing total products:
"select * from crop_types"
How I can correctly sum total products in each crop type?