首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 5角5行删除操作

Laravel 5角5行删除操作
EN

Stack Overflow用户
提问于 2018-04-13 14:41:58
回答 1查看 94关注 0票数 0

我有一个Laravel5.5和Angular5项目,其中Angular使用来自Laravel的API。product.component.html下的代码为:

代码语言:javascript
复制
<button (click)="deleteProduct(product.id)">Delete This</button>

product.component.ts下的代码如下:

代码语言:javascript
复制
deleteProduct(id){
      console.log('product ID= '+id);
      this.productService.getdeleteProduct(id);

}

在product.service.ts中,我调用了

代码语言:javascript
复制
getdeleteProduct(id): Observable<any>{
  console.log("get p id ="+ id);
  const url = `${this.url}/api/product/`+id;
  console.log(url); // http://localhost:8000/api/product/8
  return this.http.delete(url,{headers: new HttpHeaders({Authorization:'Bearer '+ this.token})});
}

下面是我的Laravel删除记录代码:

代码语言:javascript
复制
public function destroy($id)
{
    $product = Product::findOrFail($id);
    $product->delete();

    return response()->json($product);
}

产品id的值在控制台中返回得很好,但是,从phpMyAdmin中,我看到对应于'id‘的记录没有被删除。

EN

回答 1

Stack Overflow用户

发布于 2018-04-13 14:49:13

我发现我忘了用product.component.ts编写代码。它应该是这样的:

代码语言:javascript
复制
deleteProduct(id){
      console.log('product ID= '+id);
      this.productService.getdeleteProduct(id).subscribe(data => {
        console.log(data);
        this.states = data.states;

      }, (err: HttpErrorResponse) => {

          this.dataInvalid = true;
          this.formSubmitting = false;
          if(err.error instanceof Error){
            console.log("haseoor");
            this.formErrors.push(err.error.message);
          } else {
              const errors = err.error;
              const items = [];
              for (const key in errors) {
                if (errors.hasOwnProperty(key)) {
                  items.push(errors[key]);
                }
              }
              for (const k in items[1]) {
                if (items[1].hasOwnProperty(k)) {
                  this.formErrors.push(items[1][k][0]);
                }
              }
            }
        }
      )

  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49810793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档