当我单击Vue.js表中的按钮时,如何从MySql数据库中删除产品?
我只是写了它,以便只在View中删除。数据库与Laravel相关。
<div class="container" id="main">
<h1>@{{title}}</h1>
<h3>Products (@{{products.length}})</h3>
<table class="table table-striped">
<thead>
<tr style="font-size: 14px;">
<th>Name</th>
<th>Price</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in products" class="prodimg">
<td><h5>@{{item.name}}</h5></td>
<td><h5>@{{item.price}}</h5></td>
<td><button v-on:click.prevent="deleteProduct(index)">Delete
Product</button></td>
</tr>
</tbody>
</table>
<script type="text/javascript">
new Vue({
el:"#main",
data:{
products:[],
title: "Products panel"
},
methods:{
deleteProduct:function(r){
this.$delete(this.products,r);
}
},
created:function(){
axios.get("/products/all").then(r=>{
this.products = r.data;
})
}
})
</script>
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\UserModel;
use App\ProductModel;
class Admin extends Controller
{
function productsPanel(){
return view('/adminproducts');
}
function products(){
return ProductModel::all();
}
}
在Laravel和Vue.js中应该为此编写什么函数?
答案 0 :(得分:0)
您需要一个可以这样调用的DELETE API。
axios.delete(`/product/${product_id}`).then(r=>{
// do something
})