Javascript 如何使用单个方法将多个数据插入数据库
正如标题所示,我想将数据插入03个表(产品、产品、订单、订单)。 事实上,我想 1-我想恢复库存产品,并从订单的产品数量中减去(Javascript 如何使用单个方法将多个数据插入数据库,javascript,vue.js,eloquent,Javascript,Vue.js,Eloquent,正如标题所示,我想将数据插入03个表(产品、产品、订单、订单)。 事实上,我想 1-我想恢复库存产品,并从订单的产品数量中减去(stock\u actuel=stock\u actuel-quantity) 2-在products_order表中,我们记录标识符产品标识符以及每个产品的价格、数量和总金额 3-在订单表中,我们记录订单信息,如编号、日期等 所有这些都在一个商店或创建。 这就是我所做的,但它不起作用 public function store(Request $request, $i
stock\u actuel=stock\u actuel-quantity
)
2-在products_order表中,我们记录标识符产品标识符以及每个产品的价格、数量和总金额
3-在订单表中,我们记录订单信息,如编号、日期等
所有这些都在一个商店或创建。
这就是我所做的,但它不起作用
public function store(Request $request, $id)
{
// update stockproduit
// $stockproduit = Produits::find($id);
$produits = collect($request->produits->stockproduit)->transform(function ($stockproduit){
$stockproduit['stock_actuel'] = $stockproduit['stock_actuel'] - $stockproduit['quantity'];
$stockproduit->update();
});
$commande = new RestauCommande;
$commande->produit_id = 'cmde-' .$id;
$produits = collect($request->produits)->transform(function ($produit) {
$produit['total'] = $produit['quantity'] * $produit['prix'];
return new CommandesProduits($produit);
});
$data = $request->except('produits');
// $data->made_by = $request->get('users_id');
// $data->tables_id = $request->get('users_id');
$data['sub_total'] = $produits->sum('total');
$data['grand_total'] = $data['sub_total'] - $data['remise'];
// $data['clients_id'] = $client->id->$request->get('clients_id');
$data = RestauCommande::create($data);
$commande->produits()->saveMany($produits);
return response()
->json([
'La Commande a été créée avec succès' => true,
'id' => $commande->id,
]);
}
我的vue
<b-modal
id="modalShow"
title="Commandes"
ref="myModalRefProduit"
hide-footer
size="lg"
>
<div>
<div class="container mt-12">
<div class="row col-md-14">
<form @submit.prevent="store" enctype="multipart/form-data">
<div class="col-md-12 mx-auto">
<table
class="table table-responsive rounded table-striped table-sm"
>
<thead>
<tr>
<th scope="col">#</th>
<!-- <th scope="col"></th> -->
<th scope="col">Produit</th>
<th scope="col">Quantité</th>
<th scope="col">Prix</th>
<th scope="col">Total</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<tr v-for="(produit, id) in cart" v-bind:key="produit.id">
<th scope="row">{{ id + 1 }}</th>
<td>
<input
class="form-control"
type="hidden"
placeholder=""
v-model="users_id"
required
autofocus
/>
</td>
<td>{{ produit.nom }}</td>
<td>
<center>{{ produit.quantity }}</center>
</td>
<td>{{ produit.prix }}</td>
<td>{{ produit.quantity * produit.prix }}</td>
<td>
<div class="cart-icons">
<button v-on:click="cartPlusOne(produit)">
<i class="fa fa-plus"></i>
</button>
<button v-on:click="cartMinusOne(produit, id)">
<i class="fa fa-minus"></i>
</button>
<button
v-on:click="cartRemoveItem(id)"
title="supprimer le produit"
>
<i class="fa fa-trash"></i>
</button>
</div>
</td>
</tr>
<tr class="font-weight-bold">
<td style colspan="4" align="right">Total</td>
<td>{{ cartTotalAmount }}</td>
</tr>
</tbody>
</table>
<div class="container">
<div class="row">
<div class="col-md-4 pl-0">
<b-button
v-on:click="store()"
variant="outline-success btn-sm btn-block"
>Valider</b-button
>
</div>
<div class="col-md-4 pr-0">
<b-button
v-on:click="$refs.myModalRefProduit.hide()"
variant="outline-dark btn-sm btn-block"
>Annuler</b-button
>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<center></center>
</b-modal>
这就是我得到的错误
错误:请求失败,状态代码为405
提前谢谢。检查您的路线,可能您的路线已到达,请将其更改为post@DerickT. 错误代码405是
方法不允许的
,这意味着您的POST
路由未在后端设置。@tony19,@Psycho您是对的,路由已获取,我将其更改为POST,但现在我在尝试获取非对象的属性“stockproduit”时遇到此错误
store() {
// alert ('test')
const data = new FormData();
data.append(
"made_by",
document.querySelector("meta[name='users_id']").getAttribute("content")
);
axios
.post("/api/commander", data )
.then((response) => {
this.produits = response.data;
// this.loading = false;
// this.form.reset();
// window.location = response.data.redirect;
})
// .then(location.reload())
.catch(function (error) {
console.log(error);
});
},