0

In laravel I want to call a function within function to make recursive.I caught the route error.how to call the function 'recursive in tableFetch'

class queryTest extends Controller
{
 public function tableFetch() {
   recursive();
 }
 function recursive(){
   //condition
 }
}

I want to do it for check the manager of the given person and then get the manager of the fetched value in query so need to do it recursive

2
  • It is not a good idea to call a controller method inside another, instead, you can use a redirect. Could you please explain why you need to do this? Commented Sep 26, 2017 at 5:34
  • 2
    Could you use $this->recursive() to call a function in same class Commented Sep 26, 2017 at 5:41

3 Answers 3

1

A controller is not a good place for this. Instead, manage it in your Person Model(or whatever you have).

Everyone has a manager. So, your model has HasOne relation to itself.

Person Model:

public function manager()
{
    return $this->hasOne(Person::class, 'manager_id');
}

Now if you need to check the manager of given person untill you meet a certain condition you can do it inside the model and get the result in the controller.

public function checkManager()
{
    $manager = $this->manager

    if (check manager)
        return $manager;

    //check for the last manager
    return $this->manager ? $this->checkManager() : null;
}

Inside controller

function index()
{
    $person = Person::find($id);

    $manager = $person->checkManager();// this will do the recursive you need
}
Sign up to request clarification or add additional context in comments.

1 Comment

what does $person = Person::find($id); returns
1

Do something like this

class queryTest extends Controller
{
  public function tableFetch() {
     $this->recursive();
}
  function recursive(){
    //condition
  }
}

Comments

0

you need to ask more precise details about your needs, because Laravel has some complications. try doing this :

class queryTest extends Controller
{
     public function tableFetch() {
         $this->recursive();
     }
     public function recursive() {
         //condition
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.