我有一个Laravel5.5和Angular5项目,其中Angular使用来自Laravel的API。product.component.html下的代码为:
<button (click)="deleteProduct(product.id)">Delete This</button>product.component.ts下的代码如下:
deleteProduct(id){
console.log('product ID= '+id);
this.productService.getdeleteProduct(id);
}在product.service.ts中,我调用了
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删除记录代码:
public function destroy($id)
{
$product = Product::findOrFail($id);
$product->delete();
return response()->json($product);
}产品id的值在控制台中返回得很好,但是,从phpMyAdmin中,我看到对应于'id‘的记录没有被删除。
发布于 2018-04-13 14:49:13
我发现我忘了用product.component.ts编写代码。它应该是这样的:
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]);
}
}
}
}
)
}https://stackoverflow.com/questions/49810793
复制相似问题