Home > database >  Pass to the method as parameters
Pass to the method as parameters

Time:08-31

I have a method with a lot of code

public function createNewObject(Request $request) 
{
  // Code...
}

There is another method that I plan to call, but how to pass it to the createNewObject method as a Request argument?

public function deleteAndCreateObject() 
{
  $this->createNewObject(???);
}

CodePudding user response:

Just type-hint it in your deleteAndCreateObject() method.

class YourController
{
    public function createNewObject(Request $request) 
    {
        // Code...
    }

    public function deleteAndCreateObject(Request $request) 
    {
        $this->createNewObject($request);
    }
}

If that—for some reason—doesn't work for you, you can always use request():

class YourController
{
    public function createNewObject() 
    {
        $request = request();
        // Code...
    }

    public function deleteAndCreateObject() 
    {
        $this->createNewObject();
    }
}
  • Related