在Laravel 5.5中保存值为零的数字字段

时间:2019-03-16 18:39:54

标签: laravel

我有一个接受产品交付的表格,即使我在日历或“备注”字段中添加数据,如果我在“数量”字段中输入0,它也不会保存在数据库中,我注意到。

enter image description here

我已经注释掉\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, iin kernel.php仍然无法正常工作。

即使我想数量为0,如何强制laravel保存数据?提前致谢!

更新

public function store(Request $request)
{
    $input = $request->all();

    $items = [];

    for ($i = 0; $i <= count($input['order_id']); $i++) {

        if (empty($input['stock_in_qty'][$i]) || !is_numeric($input['stock_in_qty'][$i])) continue;

        $acceptItem = [
            'order_id'      => $input['order_id'][$i],
            'product_id'    => $input['product_id'][$i],
            'order_item_id' => $input['order_item_id'][$i],
            'delivery_date' => $input['delivery_date'][$i],
            'company_id'    => $input['company_id'][$i],
            // 'stock_in_qty' => intval($input['stock_in_qty'])[$i],
            'stock_in_qty'  => $input['stock_in_qty'][$i],
            // 'stock_out_qty' => $input['stock_out_qty'][$i],
            // 'transfer_to' => $input['transfer_to'][$i],
            'delivery_note' => $input['delivery_note'][$i],
            'user_id'       => $input['user_id'][$i],
        ];
        array_push($items, Warehouse1stocks::create($acceptItem));

        $stockSummary = Warehouse1StockSummaries::firstOrCreate(
            ['product_id' => $input['product_id'][$i]],
            ['qty_in'  => $input['stock_in_qty'][$i],
             'qty_out' => null,
            ]);

        if (!$stockSummary->wasRecentlyCreated) {
            $stockSummary->increment('qty_in', $input['stock_in_qty'][$i]);
        }
    }

    if ($input['rd'] == $input['stock_in_qty'] || $input['rd'] == 0) {
        $order_idAcceptedItem = $acceptItem['order_id'];
        $setStatus = \App\Orders::where('id', '=', $order_idAcceptedItem)->first();
        if ($setStatus) {
            $setStatus->status_id = 4;
        }
        $setStatus->save();
    } else {
        $order_idAcceptedItem = $acceptItem['order_id'];
        $setStatus = \App\Orders::where('id', '=', $order_idAcceptedItem)->first();
        if ($setStatus) {
            $setStatus->status_id = 3;
        }
        $setStatus->save();
    }

    return redirect()->route('orders.index');
}

1 个答案:

答案 0 :(得分:2)

empty()将返回带有true0的{​​{1}},这意味着如果您尝试将数量更改为0,'0'循环将继续进入下一个循环。如果需要检查值是否存在,可以改用for

只需将以下内容更改为您的第一个if语句:

isset()