Reputation: 33
Thank you for taking the time to read this question. I have searched everywhere and cannot find the answer.
I am learning Laravel and are making a simple blog. I created a resource controller like so:
Route::resource('blog-posts', 'AdminBlogPostsController');
I was able to get all actions working fine (index, show, create, edit, update, delete) without any issues.
Since each blog post belongs to a category, I wanted the user to filter the blog-posts/index view by category, so I added this named route, BEFORE the resource declaration:
Route::get('blog-posts/{category_id?}', 'AdminBlogPostsController@index');
Route::resource('blog-posts', 'AdminBlogPostsController');
At this point, the user is able to filter by category just fine. However, if I navigate to
blog-posts/create
The browser simply displays blog-posts/index route, and I am unable to create a new record. If I comment out the first route like so:
//Route::get('blog-posts/{category_id?}', 'AdminBlogPostsController@index');
Route::resource('blog-posts', 'AdminBlogPostsController');
I am able to create blog posts again, but I am unable to filter the view.
Any ideas how to begin debugging this?
Upvotes: 0
Views: 48
Reputation: 2032
Try adding a where condition at the end of the category route like so
Route::get('blog-posts/{category_id?}', 'AdminBlogPostsController@index')->where('category_id', '[0-9]+');
Basically this tells laravel to route to the index method if the second segment of the url is a digit.
Take a look at the Laravel documentation page for some more examples of this.
Upvotes: 2