Reputation: 13
I'm trying to create eshop (i want to learn laravel) with some form to add data.
I have two routes, first for display product(product/{id}) and second to create new product(product/create). But when i try to display some product, it shows 404, same as on second site.
When i delete second route, everything works, but together they don't work.
Route::group(['middleware' => 'web'], function () {
// Products
Route::get('/product/', 'ProductController@index')->name('product.all'); //All products
Route::get('/product/{id}/', 'ProductController@show')->name('product.id'); // Only one product with id
Route::group( ['middleware' => 'auth'], function() {
// Products
Route::get('/product/create/', 'ProductController@create');// Form to add new product
});
});
When i go to url /product/8 i want to show product with id 8, but on url /product/create i want to show form to create new product
Upvotes: 0
Views: 54
Reputation: 1379
You have to move your create route at the top because your route /product/create
overrides the /product/{id}
Change your routes just like below:
Route::group(['middleware' => 'web'], function () {
// Products
Route::group( ['middleware' => 'auth'], function() {
// Products
Route::get('/product/create/', 'ProductController@create');// Form to add new product
});
Route::get('/product/', 'ProductController@index')->name('product.all'); //All products
Route::get('/product/{id}/', 'ProductController@show')->name('product.id'); // Only one product with id
});
Upvotes: 1
Reputation: 834
you don't need to add the web middleware since, it's added to all routes in web.php
// Products
Route::get('/product/', 'ProductController@index')->name('product.all'); //All products
Route::group( ['middleware' => 'auth'], function() {
// Products
Route::get('/product/create/', 'ProductController@create');// Form to add new product
});
Route::get('/product/{id}/', 'ProductController@show')->name('product.id'); // Only one product with id
Upvotes: 1
Reputation: 14268
This happens because your second route meaning /product/create
overrides the first one, so you either move the /product/{id}
route below the create route, or you can always add regex
constraint on the route like this:
Route::get('/product/{id}/', 'ProductController@show')
->name('product.id')
->where(['id' => '[0-9]+']);
Upvotes: 2