Bullfinch
Bullfinch

Reputation: 255

Redirect to named route with parameters in Laravel

I want to redirect to routes that are named and be able to use parameters in that route.

This will work but i dont want to specify the route as a specific string

return Redirect::to('admin/user/' . $id . '/edit')

I want to use the routes name, something like this:

return Redirect::route('user/edit')

But that gives me the error:

Trying to get property of non-object

And the wrong route:

admin/user/%7Bid%7D/edit

I have specified the route and named it in my routes.php file

Route::get('/admin/user/{id}/edit', array(
    'as' => 'user/edit',
    'uses' => 'UserController@edit'
));

Upvotes: 1

Views: 2491

Answers (1)

Damien Pirsy
Damien Pirsy

Reputation: 25435

You need to tell the Redirector wich parameter(s) you want to substitute, or it will use the 'default' you specified: in this case {id} (%7B and %7D are the encoded { and } respectively)

return Redirect::route('user/edit', array($id));

The method args are:

public RedirectResponse route(string $route, array $parameters = array(), int $status = 302, array $headers = array())

Upvotes: 4

Related Questions