在laravel中,类illuminate\database\eloquent\collection的对象无法转换为int

ia2d9nvy  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(372)

我有一个名为purchase\u details的表,在这个表中,在购买过程中,我一次存储许多项目的购买记录。在采购过程中,我还根据采购项目id更新项目表列期初余额,现在我在尝试将“采购明细”表数量的值与“项目”表旧期初余额相加时遇到问题-在控制器中,我尝试类似的操作-

public function store(Request $request)
{
$grandTotal = $request->input('grand_total');
$paidAmount = $request->input('paid_amount');
$purchase = new Purchase;

$purchase->no          = $request->input('no');
$purchase->purchase_date = Carbon::parse($request->purchase_date)->format('Y-m-d');
$purchase->notes       = $request->input('notes');
$purchase->supplier_id = $request->input('supplier');
$purchase->total_quantity = $request->input('total_quantity');
$purchase->grand_total = $grandTotal;
$purchase->paid_amount = $paidAmount;
$purchase->due_amount  = abs($grandTotal - $paidAmount);
$purchase->save();

$itemDetails = [];
$itemIds = $request->input('itemIds');
$itemQuantities = $request->input('itemQuantities');
$itemPrices = $request->input('itemPrices');
$itemTotals = $request->input('itemTotals');
$orderNotes = $request->input('orderNotes');

foreach ($itemTotals as $key => $total) {
    $itemDetails[] = [
        'item_id' => $itemIds[$key],
        'quantity' => $itemQuantities[$key],
        'unit_price' => $itemPrices[$key],
        'total_price' => $itemTotals[$key],
    ];
    $openingBalance = Item::where('id', $itemIds[$key])->get(['opening_balance']);
    DB::table('items')
                ->where('id', $itemIds[$key])
                ->update(['opening_balance' => $openingBalance + $itemQuantities[$key]]);
}
$purchase->purchaseDetails()->createMany($itemDetails);

return back();
}
ego6inou

ego6inou1#

使用集合作为int,编辑代码:

$openingBalance = Item::select(['opening_balance'])->where('id', $itemIds[$key])->first()->opening_balance;

相关问题