Error 500 (Internal Server Error) ajax and laravel

后端 未结 1 1198
青春惊慌失措
青春惊慌失措 2021-01-15 05:57

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

相关标签:
1条回答
  • 2021-01-15 06:30

    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.

    1. 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']);
      
    2. 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.

    0 讨论(0)
提交回复
热议问题