1

im working with laravel and php, im doing a recursive function, the problem is that the return value of the function doesnt work just work echo.

The code is the this.

public function getSitioPadre($id){
    $padre =  md_sitio_espacios::where([['SITIO_ESPACIOS_ID','=',$id]])->get()->toArray();       
    if($padre[0]["PADRE"] == 0){
        return $padre[0]["SITIO_ESPACIOS_ID"];
    }else{
        $this->getSitioPadre($padre[0]["PADRE"]);
    }
}

When i call this function like :

echo $this->getSitioPadre(54);

returns nothing,

If in the function i use : echo $padre[0]["SITIO_ESPACIOS_ID"]instead of return it works.

Let me know what can i do, thank you.

bjesua
  • 319
  • 1
  • 4
  • 14

1 Answers1

4

You don't appear to return anything in your else clause.

public function getSitioPadre($id){
    $padre =  md_sitio_espacios::where([['SITIO_ESPACIOS_ID','=',$id]])->get()->toArray();       
    if($padre[0]["PADRE"] == 0){
        return $padre[0]["SITIO_ESPACIOS_ID"];
    }else{
        return $this->getSitioPadre($padre[0]["PADRE"]); // should work better
    }
}
Calimero
  • 4,238
  • 1
  • 23
  • 34