Axios 不会从 empyt 数组 Vuejs 和 Laravel 中删除数据



我的代码有问题。我的 axios 删除请求不会从数据库中删除数据。我得到回复 200,但仅此而已。

元件

    deleteComment(index){
      axios.delete(this.uri + this.comments[index].id)
      .catch(error=>{
        console.log(error)
      });
    }

控制器

  public function destroy(BlogComments $cid){
    $comments->delete();
    return response()->json([
      'comments'=> $id,
      'Message'=> "OK!"
    ]);
  }

控制台 -> 网络

Request URL: http://127.0.0.1:8000/api/blog-comments/4
Request Method: DELETE
Status Code: 200 OK
Remote Address: 127.0.0.1:8000
Referrer Policy: no-referrer-when-downgrade

当我制作响应表单控制器时,我只有空数组

comments[]

更新

我修好了。

我替换了

Route::resources('/blog-comments', 'DashboardBlogCommentsController');

Route::delete('/blog-comments/{id}', 'DashboardBlogCommentsController@destroy');

为什么资源不起作用?我在另一个页面中使用了相同的方法,它有效

Route::resource('/advantages', 'DashboardAdvantagesController');
  public function destroy(HomepageAdvantages $advantage){

  $advantage->delete();
  return response()->json([
    'advantage'=>$advantage,
    'message'=> 'task created'
  ]);

  }

也许是以下问题的答案:

为什么资源不起作用?

路线

这可能是您的编辑中的拼写错误,

而不是您的代码中的拼写错误,但不是resources

Route::resources('/blog-comments', 'DashboardBlogCommentsController');

resource

Route::resource('/blog-comments', 'DashboardBlogCommentsController');

并根据您的问题更新前的代码:

控制器DashboardBlogCommentsController

public function destroy(BlogComments $comment){ 
    // use delete() on the variable where you assigned the object
    $comment->delete();
    return response()->json([ 
        // 'comments'=> $id, this no needed, the comment doesn't exist anymore
        'message' => 'OK!' ], 202);
}

希望对您有所帮助

最新更新