Cristal
Cristal

Reputation: 502

Laravel error: NotFoundHttpException in RouteCollection.php line 161 (resource)

If I use the below method, it works fine.

Route::get('create', function () {
    return view('post.create');
});

However, if I use the resource, it gives me the below errors:

Route::resource('posts', 'PostController');

It gives the below error

NotFoundHttpException in RouteCollection.php line 161

Upvotes: 2

Views: 175

Answers (2)

Adarsh Sojitra
Adarsh Sojitra

Reputation: 2199

You are trying to create the Post using Resource Controller.

Your URL or Route should be posts/create and not posts because posts is used to get the List of the posts which will fire index method from your Resource Controller.

To Create, your Route must be posts/create and it will file create method from your Resource Controller.

It means,

Route::resource('posts','PostsController'); => Route::get('posts','PostsController@index);


AND

Route::resource('posts/create','PostsController'); => Route::get('posts/create','PostsController@create);


Let me know if that works!

Upvotes: 0

EddyTheDove
EddyTheDove

Reputation: 13259

Because Route::resource('posts', 'PostController'); generates

Route::get('posts/create', 'PostController@create');

NOT

Route::get('create', 'PostController@create');

This means you have to link to 'post/create'

<a href="/posts/create">New Post</a>

Upvotes: 2

Related Questions