I assume the TV Series Mahabharat had the category_id 4.
Once you have that you can iterate through the categories until you find a category with the parent 0.
For example. :
public function getCategories($record)
{
$category = "";
$buildCategory = true;
$id = $record['category_id'];
while ($buildCategory)
{
$category = $this->getCategoryById($id); //Don't use query here, that's not efficient.
if ($category['parent'] != 0) {
$category .= $category['name'] . '->';
} else {
$category .= $category['name'];
$buildCategory = false;
}
}
return $category;
}