The Problem
You're trying to redirect to a route by it's name, but you're passing an URL to the method.
return redirect() # Initialize a redirect...
->route(...); # to a route by it's name
The solutions
There are multiple ways to solve this. You can either redirect by path (that's
how you're trying to do at the moment), or by using route names.
Method 1: Redirect by path
You can redirect by path in two ways:
- concat the path by hand
- use the URL facade.
Concat the path by hand
$url = '/orders/view/' . $order->id;
return redirect($url);
Use the URL facade
# Remember to remove the trailing slash.
# Appending a trailing slash would lead to example.com/orders/view//1
$url = URL::to('/orders/view', [$order->id]);
return redirect($url);
You're done!
Method 2: Redirect by route name
To use route names, you need to prepare your routes and give them a name.
I recommend this way, because sometimes it's more clear but it also makes
maintaining your application easier when you need to change some paths, because
the names will be still the same. Read more about it here:
https://laravel.com/docs/5.2/routing#named-routes
This also allows you to use the easy-to-use and useful route()
helper method.
Prepare your routes file
To assign your routes a name, you need to pass them to your router in your app/Http/routes.php
.
# Example 1
Route::get('/orders/view/{id}', [
'as' => 'orders.view', 'uses' => 'OrderController@view'
]);
# Example 2
Route::get('/orders/view/{id}', 'OrderController@view')->name('orders.view');
For myself, I recommend to use the second syntax. It makes your code more
readable.
Redirect by route name
Now it's time to redirect! If you're using the method of route naming, you don't
even have to change much in your code.
return redirect()->route('orders.view', ['id' => $order->id]);
A note from my side
Have a look on route-model-binding in the documentation:
https://laravel.com/docs/5.2/routing#route-model-binding
This allows you to pass a model instance directly to the route()
method and
it helps you fetching models by passing the proper model to your controller method
like this:
# Generate route URL
route('orders.view', $order);
# Controller
public function view(Order $order) {
return $order;
}
And in your app/Http/routes.php
you would have {order}
instead of {id}
:
Route::get('/orders/view/{order}', 'OrderController@view');