我有一个输入字段,并且此输入字段是Multiple input。我在将此输入存储到数据库时遇到问题,因为我认为在创建此查询输入时出错。它是我的输入字段
<form>
<button wire:click.prevent="add()">
Add
</button>
@foreach($inputs as $key => $value)
<input type="hidden" wire:model="inputs.{{ $key }}.newid" value="{{ $key }}">
<input wire:model="inputs.{{ $key }}.nama_barang" type="text" />
<input wire:model="inputs.{{ $key }}.qtt" type="text" />
<input wire:model="inputs.{{ $key }}.price" type="text" />
<input wire:model="inputs.{{ $key }}.qty" type="text" />
<input value="{{ (int)$value['price'] * (int)$value['qty'] }}" type="text" />
<br>
@endforeach
<button wire:click.prevent="store()">Submit</button>
及其我的livewire功能
public $belanja_id, $nama_barang, $qtt,$newid;
public $updateMode = false;
public $i = 1;
public $total ;
public $price= [] ;
public $qty = [];
public $inputs = [
[
"newid" => "",
"nama_barang" => "",
"qtt" => "",
"price" => "",
"qty" => "",
"total_price" => "",
]
];
public function add($i)
{
array_push($this->inputs, [
"newid" => "",
"nama_barang" => "",
"qtt" => "",
"price" => "",
"qty" => "",
"total_price" => "",
]);
}
public function store()
{
foreach ($this->inputs as $key => $value) {
$bel = AnakBelanja::create([
'belanja_id' => $this->newid,
'nama_barang' => $this->nama_barang[$key],
'qtt' => $this->qtt[$key],
'price' => $this->price[$key],
'qty' => $this->qty[$key]
]);
}
$this->inputs = [];
$this->resetInputFields();
return redirect()->route('detail', $bel->belanja_id);
}
此输入有错误
尝试访问类型为null的值的数组偏移量
有人可以帮忙吗?谢谢
答案 0 :(得分:0)
我认为缺少变量$i
,您的add
函数应类似于以下代码,更改后应存储所有数据。
public function add($i)
{
$i = $i + 1;
$this->i = $i;
array_push($this->inputs ,$i);
}