29

I'm developing a Laravel 5 app, I have this route

Route::get('states/{id}/regions', ['as' => 'regions', 'uses' => 'RegionController@index']);

In my controller, after I make a post call correctly, I want to redirect to that view, with this command:

return \Redirect::route('regions')->with('message', 'State saved correctly!!!');

The problem is that I don't know how can I pass {id} parameter, which should be in my URL.

Thank you.

Bellots
  • 1,703
  • 3
  • 19
  • 29

8 Answers8

55

You can pass the route parameters as second argument to route():

return \Redirect::route('regions', [$id])->with('message', 'State saved correctly!!!');

If it's only one you also don't need to write it as array:

return \Redirect::route('regions', $id)->with('message', 'State saved correctly!!!');

In case your route has more parameters, or if it has only one, but you want to clearly specify which parameter has each value (for readability purposes), you can always do this:

return \Redirect::route('regions', ['id'=>$id,'OTHER_PARAM'=>'XXX',...])->with('message', 'State saved correctly!!!');
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • As you can see, my Redirect::route has already a 'message' parameter. How can I add the $id parameter, that should be placed in URL, and also the 'message' parameter? – Bellots May 03 '15 at 22:12
  • 2
    @Bellots In the example shown, you can see the $id is passed in as the second parameter to the `route()` method. This second parameter is what is used to build the route. Any "parameters" you add with the `with()` method is actually just data being flashed into the Session for you to access on your redirect. So, your full line would be: `return \Redirect::route('regions', [$id])->with('message', 'State saved correctly!!!');` – patricus May 04 '15 at 02:58
  • 2
    try this `return redirect('states/'.$id.'/regions')->with(['message' => 'State saved correctly!!!']);` – Smith Foto May 04 '15 at 04:04
  • 1
    @SmithFoto, i would not recommend doing so. I prefer using the array stuff, as it adds more readability. – Lauro Wolff Valente Sobrinho Dec 03 '16 at 12:15
22

You could still do it like this:

return redirect()->route('regions', $id)->with('message', 'State saved correctly!!!');

In cases where you have multiple parameters, you can pass the parameters as an array, for example: say you had to pass the capital of a particular region in you route, your route could look something like the following:

Route::get('states/{id}/regions/{capital}', ['as' => 'regions', 'uses' => 'RegionController@index']);

and you can then redirect using:

return redirect()->route('regions', ['id' => $id, 'capital' => $capital])->with('message', 'State saved correctly!');
Bowis
  • 541
  • 8
  • 31
Awa Melvine
  • 3,797
  • 8
  • 34
  • 47
2

There are several ways to redirect this URL in laravel:

  1. Using URL with a global redirect helper function

    return redirect('states/'.$id.'/regions')->with('message', 'State saved correctly!!!');  
    
  2. Using named route

    return redirect()->route('regions', ['id' => $id])->with('message', 'State saved correctly!!!');
    
  3. Using controller action

    return redirect()->action('RegionController@index', ['id' => $id])->with('message', 'State saved correctly!!!');
    
Community
  • 1
  • 1
1

You can use the following

return redirect(route('addPost1',['pid',$post->id]));

OR

return redirect(route('addPost1'))->with('pid',$post->id);
Siddhartha
  • 155
  • 1
  • 3
0

You can pass {id} parameter with redirection like that

return \Redirect::route('regions', [$id])->with('message', 'State saved correctly!!!');
0

If the router contains like this :

Route::get('/displayCustomer/{id}','AdminController@displayCustomer')->middleware('auth','admin');

and in the controller redirection can be done like this

    public function displayCustomer($id){
        $user = DB::table('customer_infos')->where('customer_id', $id)->first();       
        return view('admin.DisplayCustomer', compact('user', $user));
    }

    public function approveCustomerInvoice(Request $request,$id)
    {
        $customer = CustomerInfo::find($id);
        $customer->status = 1;
        $customer->save();

       return redirect()->action('AdminController@displayCustomer', ['id' => $id])->with('message', 'Customer Invoice Approved!!!');
    }
0

You can assign a variable outside with url and id and call it inside redirect.

$url = 'view_customer/' . $id;
        return redirect($url)->with('message', "Customer Page");
Ajith
  • 3
  • 3
0

Above answer already shown multiple way to do. just adding another way to achieve that in compact way.

return Redirect::route('regions', [$param1,$param2,$param3])->with('message', 'State saved correctly!!!');

pass in order and access in same order.

Route::get('/displayCustomer/{$param1}/{param2}/{param3}','AdminController@displayCustomer')->middleware('auth','admin');
Amit
  • 1,841
  • 1
  • 19
  • 36