当我插入 2 个数据时尝试总是捕获 return 错误
Try catch always return error when I insert 2 data
我想添加功能检查数量库存如果我添加 1 个数据它可以工作但是在我插入 2 个数据之后它会出错这里是我的控制器
$i = 0;
$sum = 0;
$count = count($request->get('quantity_box'));
$a = "INV".rand(10,99).$ldate.rand(10,9);
$errors = false;
try {
DB::transaction(function () use ($count,$request,$a,$errors) {
for ($i=0; $i < $count; $i++) {
print_r($i);
$warehouse_products_sell = New Warehouse_products_sell;
$id_w = $request->input('idw');
$id_c = $request->get('id_c')[$i];
$id_p = $request->get('id_p')[$i];
$qty = $request->input('quantity_box')[$i];
$price = $request->input('price')[$i];
$available = $this->check_stock($id_w, $id_p, $qty);
if($available > 0){
$warehouse_products_sell->add_by = $request->input('add_by');
$warehouse_products_sell->save();
}else{
throw new \Exception('Error');
}
}
});
} catch (\Exception $e) {
$errors = $e;
}
当我打印 $errors
时,它出现了。 'Exception' 留言 'Error'
我尝试通过删除 try catch 并添加 2 个数据来检查此查询,它工作正常。不确定如何检查此错误
这是我的 html 表单输入
<input type="text" name="quantity_box[]" class="form-control" autofocus="" />
好吧我想通了
在我的 html 中应该
<input type="text" name="quantity_box[0]" class="form-control" autofocus="" />
我想添加功能检查数量库存如果我添加 1 个数据它可以工作但是在我插入 2 个数据之后它会出错这里是我的控制器
$i = 0;
$sum = 0;
$count = count($request->get('quantity_box'));
$a = "INV".rand(10,99).$ldate.rand(10,9);
$errors = false;
try {
DB::transaction(function () use ($count,$request,$a,$errors) {
for ($i=0; $i < $count; $i++) {
print_r($i);
$warehouse_products_sell = New Warehouse_products_sell;
$id_w = $request->input('idw');
$id_c = $request->get('id_c')[$i];
$id_p = $request->get('id_p')[$i];
$qty = $request->input('quantity_box')[$i];
$price = $request->input('price')[$i];
$available = $this->check_stock($id_w, $id_p, $qty);
if($available > 0){
$warehouse_products_sell->add_by = $request->input('add_by');
$warehouse_products_sell->save();
}else{
throw new \Exception('Error');
}
}
});
} catch (\Exception $e) {
$errors = $e;
}
当我打印 $errors
时,它出现了。 'Exception' 留言 'Error'
我尝试通过删除 try catch 并添加 2 个数据来检查此查询,它工作正常。不确定如何检查此错误
这是我的 html 表单输入
<input type="text" name="quantity_box[]" class="form-control" autofocus="" />
好吧我想通了
在我的 html 中应该
<input type="text" name="quantity_box[0]" class="form-control" autofocus="" />