2
votes

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

2

2 Answers

2
votes

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>
0
votes

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!