James Parsons James Parsons - 1 month ago 9
PHP Question

Redirecting with parameters to a controller in Laravel and picking up the parameters in the second controller

I am looking to redirect with parameters to a controller in Laravel and picking up the parameters in the second controller.

I am set up in the first controller as per the docs, link below.

https://laravel.com/docs/5.3/redirects#redirecting-controller-actions

The Redirect

return redirect()->action(
'quote', ['enquiry' => $request->final_enquiry_id]
);


The Route

Route::post('/quote', [
'as' => 'quote',
'uses' => 'Quote_Controller@quote'
]);


The second controller

public function quote(Request $request)
{
echo $enquiry;


However this currently returns undefined variable.

Is this the best way to do this and if so where am I going wrong?

Answer

Your second controller should be as:

public function quote($enquiry, Request $request)
{
  echo $enquiry;

And your route should be as:

Route::post('/quote/{enquiry}', [
  'as' => 'quote',
  'uses' => 'Quote_Controller@quote'
]);

Or if want your enquiry param to be optional then you can do it as:

public function quote($enquiry = null, Request $request)
{
  echo $enquiry;

And your route should be as:

Route::post('/quote/{enquiry?}', [
  'as' => 'quote',
  'uses' => 'Quote_Controller@quote'
]);