【发布时间】:2021-05-14 08:50:48
【问题描述】:
我是 Vue js 的新手,我试图到处寻找我的问题的答案,但徒劳无功。 我只是尝试从 Vue 组件向 Laravel 发送一个数组(购物车),但是当我尝试使用 foreach 循环获取数组中的项目时,我只得到 [object Object]。我做错了什么? 我应该能够获得标题名称、数量和价格等,这一切都在我的网站中的 Vue 组件中工作,但是当我尝试发送到 Laravel 时,无法阅读它。
这是我的一些代码,请给我建议。
<form action="../api/checkout" method="post" @submit="checkout">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Artiklar</th>
<th scope="col">Antal</th>
<th scope="col">Pris</th>
<th scope="col">Summa</th>
<th scope="col">Radera</th>
</tr>
</thead>
<tbody>
<input type="hidden" name="cartobj" v-bind="cartitem">
<tr v-for="(crt, index) in cart" :key="index">
<td>{{ crt.title }}<br><img :src="/image/+crt.image" :alt="crt.image" style="height: 80px;"></td>
<td>{{ crt.qty }} st <br><div class="row">
<button class="btn btn-link col-md-6" @click="decrease(index)">-</button>
<button class="btn btn-link col-md-6" @click="increase(index)">+</button>
</div></td>
<td>{{ crt.subprice }} kr</td>
<td>{{ (crt.subprice*crt.qty).toFixed(2) }} kr</td>
<td><button @click="removecart(index)" class="btn btn-link"><img src="/pic/trash.png" alt="" width="20"></button></td>
</tr>
</tbody>
</table>
<p class="border rounded text-right p-2"><b>Total: {{ subsum }} kr</b></p>
<button id="checkout" class="btn btn-success col">Gå till kassan</button>
</form>
export default {
props: ['product'],
data() {
return {
cartitem: {
title: this.product.article,
qty: 1,
subprice: (this.product.price/100).toFixed(2),
image: this.product.image
},
cart: []
}
},checkout() {
let c = {cartobj: this.cart};
axios.post('../api/checkout', JSON.stringify(c));
}
从我的控制器中,我尝试了很多不同的东西,但无法让它工作,即使是嵌套的 foreach 循环......
public function checkout(Request $request)
{
$cart = request();
foreach($cart as $d=>$c) {
echo $d;
}
die();
}
【问题讨论】:
-
要准确查看 Vue 应用程序向服务器发送的内容,您可以使用浏览器开发控制台中的网络工具。以下是如何为 Chrome 执行此操作的示例:developers.google.com/web/tools/chrome-devtools/network
-
非常感谢,我会调查一下再回来。
-
阵列看起来还可以,但我认为现在问题出在服务器端...
-
你可能有幸使用
$cart = $request->input('cartobj');获取从checkout函数发送的cartobj: this.cart信息 -
我也会尝试这个解决方案。谢谢