9

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.

7 Answers 7

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

the issue with this in Laravel 7 (as I have only tested it on Laravel 7) is that it returns the whole request object itself. example of a returned object "attributes": {}, "request": {}, "query": {}, "server": {}, "files": {}, "cookies": {}, "headers": {}, "id": 1, "amount": 20, "comment": "deduction"
4

Change this line

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

To

$report ->Applications($request);

2 Comments

this will pass all the request parameters but I want to only send some of them
may be you can do this by making a class which implements/extends Illuminate/http/Request to filter data
3

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.

Comments

1

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*/
}

2 Comments

this will send all the request parameters but I wanted to send only some paramenters like on the getquickviews I receive 4 but I only wanted to send 3 to Applications function
$returnedRequest = $request; // do whatever with your request here here you do whatever with your first request. So if you want only 3 parameters, set the 3 parameters in $returnedRequest
0

I think this will work :

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

Comments

0

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!!!

Comments

-1

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);

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.