S.M_Emamian S.M_Emamian - 1 year ago 96
PHP Question

recursive function in model doesnt return anything - laravel

I made a function inside

model :

public function getSecondCategoryByID($category_id = 0,$id = 0)

$category = $this->where('id','=',$category_id)->first();

if($category->parent_cat_id == 1)

$mcategory = $this->where('id','=',$id)->first();

echo 'Inside Model '.$mcategory->id;
return $mcategory->id;



at a function of controller I called this function :

$category = new Category;

$secondCategoryID = $category->getSecondCategoryByID($category_brand->category_id,0);

echo 'After Model '.$secondCategoryID ;
return ;

when I call my function of controller I got this message on page :

Inside Model 3After Model //it should be Inside Model 3After Model '3'

I think my model doesn't return anything ! because after
after model
is nothing !!!!!

Answer Source

When doing recursion you need to return the recursive call also, here shown by changing the else block statement.

    return $this->getSecondCategoryByID($category->parent_cat_id,$category->id);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download