I have a categories table with parent_id column, referring to parent category's ID. I need to retrieve all of the ID's that are under a certain category (all of its children & grandchildren) I wrote this recursive function but for some reason it only returns the direct children of the given category id.
What am I missing here?
function subCategoriesList($id, $data=[]){
$children = Category::whereParentId($id)->get();
foreach ($children as $child){
$data[] = $child->id;
subCategoriesList($child->id, $data);
}
return $data;
}