Mohd Mobeen
Mohd Mobeen

Reputation: 95

How to pass instance of Request from a controller function to another controller function

I have to call a function from one controller an other controller.

public function getquickviews(Request $request){
     $report = new ReportController();
     $report ->Applications($request->except('cl_e_start_date'));//it's not working its giving me error that it expect and instance of Request and passed array()
}



    public function Applications(Request $request) 
    {
/*APP USAGE*/
     }

and I have to pass instance of Request to Application function. But the issue I don't wanted to pass all the parameter from getquickviews Request like if I am getting email,phone,name on the getquickviews function but I only have to pass phone,email to Application function.

Upvotes: 9

Views: 18734

Answers (7)

stanley mbote
stanley mbote

Reputation: 1306

To be able to create a custom request and thus use it to reference a post method in a controller, you need to first initiate an instance of Request as @martin Carrasco has described above:

the code below is a continuation of martin Carrasco

public function getquickviews(Request $request){
$report = new ReportController();
$content = new Request
([
     'firstParam' => $request->param1,
     'secondParam' => $request ->param2,
]);

 $report ->Applications($content);

}

Try that n hope it works.

Upvotes: 3

martin carrasco
martin carrasco

Reputation: 141

You need to create a new instance of Request.

public function getquickviews(Request $request){
 $report = new ReportController();
 $content = new Request();
 $content->something = $request->something;
 $content->somethingElse = $request->somethingElse;
 $report ->Applications($content);
 }

and then you have to recieve it in:

public function Applications(Request $request) 
{
/*APP USAGE*/
 }

and that's it. Regards.

Upvotes: 14

Gautam Patadiya
Gautam Patadiya

Reputation: 1411

Two ways to get requests into next method or any next level call.

First you can inject Request class depenednacy into that method for an example:

public function store(Request $request)
{
    // Form Submits here
}

If you want to pass $request into other method for example to display data after insert you can do this way:

public function showStore(Request $request)
{
    dd($request->get());
}

Then you can call this method from store method

$this->showStore($request);

or second is you can use request as metho into showStore or any n level call. Like this:

public function showStore()
{
    dd(request()->all());
}

$this->showStore(); // You do not require any injection.

Good Luck!!!

Upvotes: 0

Naveen Kumar
Naveen Kumar

Reputation: 1626

You can keep the particular key and value you want and delete the rest from the $request before passing it to the function. First convert the $request to array by

$request->toArray()

and then delete the unwanted keys by doing

unset($request['key-here']);

and then pass it to the function

$report ->Applications($request);

Upvotes: -1

vikalp
vikalp

Reputation: 330

I think this will work :

$report ->Applications($request->toArray());

Upvotes: 0

kevinniel
kevinniel

Reputation: 1106

try as following (not sure it's gonna work) :

public function getquickviews(Request $request){
    $returnedRequest = $request; // do whatever with your request here
    return redirect()->route('SecondController.Applications', compact('returnedRequest'));
}



public function Applications(Request $request){
    /*APP USAGE*/
}

Upvotes: 1

Sohel0415
Sohel0415

Reputation: 9853

Change this line

$report ->Applications($request->except('cl_e_start_date'));

To

$report ->Applications($request);

Upvotes: 4

Related Questions