Hello people I am facing a problem with my comments system in laravel and ajax. Actually it works only with php but i\'m having problems with ajax.
This is the error
It appears my(and Anton's) hunch was correct. You have two conflicting routes.
Route::post('comments/{post_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);
And of course
Route::post('comments/', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
Because the two routes use roughly the same route, laravel just goes by which is defined first, which is your comments.store
route.
There are a couple ways to fix this.
Change the order of your routes:
Route::post('comments/update', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
Route::post('comments/{post_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);
Route::get('comments/{id}/edit', ['uses' => 'CommentsController@edit', 'as' => 'comments.edit']);
Use route constraints:
Route::post('comments/{post_id}', [
'uses' => 'CommentsController@store',
'as' => 'comments.store'
])->where(['post_id' => '[0-9]+']);;
Route::get('comments/{id}/edit', ['uses' => 'CommentsController@edit', 'as' => 'comments.edit']);
Route::post('comments/update', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
Of note, I don't know how the Facade registrar handles the casing(lower, upper) of facade methods.. So in an effort to not cause further bugs, I used the lower casing of POST
, just as it is used in the documentation.