我的控制器
$this->validate($request, [
'master_item_id.*' => ['required'],
'quantity.*' => ['required', 'numeric', 'gt:0']
]);
$master_item_id = array_map('intval', $request->master_item_id);
$quantity = array_map('intval', $request->quantity);
$price = MasterItem::whereIn('id',$master_item_id)->get('price');
$transaction = Transaction::create([
'total_price' => 1,
'last_edited_by' => Auth::user()->name
]);
foreach ($master_item_id as $key => $no) {
$input['transaction'] = $transaction->id;
$input['master_item_id'] = $no;
$input['quantity'] = $quantity[$key];
$input['price'] = $price[$key];
TransactionItem::create($input);
}
我的模型
protected $table = 'transaction_item';
protected $guarded = [];
public $timestamps = false;
public function transaction()
{
return $this->belongsTo(Transaction::class, 'transaction_id');
}
public function item()
{
return $this->belongsTo(MasterItem::class, 'master_item_id');
}
當我創建時,彈出這個錯誤。
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '{"price":1000000}' for column `myDatabase`.`transaction_item`.`price` at row 1
我知道問題是因為我的$price,但我不知道如何解決這個問題,有沒有更好更簡單的方法來解決我的問題?謝謝!
uj5u.com熱心網友回復:
很確定問題是
$input['price'] = $price[$key];
因為MasterItem::whereIn('id',$master_item_id)->get('price');會回傳如下內容:
[{價格:X},{價格:Y} ...]
而不是像
[X, Y]
試試這個:
$input['price'] = $price[$key]['price'];
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/312813.html
