【问题标题】:Axios doesn't delete data from database - empyt array Vuejs and LaravelAxios 不会从数据库中删除数据 - 空数组 Vuejs 和 Laravel
【发布时间】:2019-10-22 10:36:36
【问题描述】:

我的代码有问题。我的 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'
  ]);


  }

【问题讨论】:

  • 在编辑之前,您没有在销毁函数上定义变量 $cmets。那么为什么会这样:$comments->delete(); ?

标签: database laravel vue.js


【解决方案1】:

也许是以下问题的答案:

为什么资源不起作用?

路线

这可能是您的编辑而不是您的代码中的拼写错误,但不是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);
}

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 2016-07-18
    • 1970-01-01
    • 2012-05-04
    • 2019-08-02
    相关资源
    最近更新 更多