如何解决Laravel Livewire存储阵列无法存储数据
我有一个输入字段,并且此输入字段是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" => "",]);
}
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的值的数组偏移量
有人可以帮忙吗?谢谢
解决方法
我认为缺少变量$i
,您的add
函数应类似于以下代码,更改后应存储所有数据。
public function add($i)
{
$i = $i + 1;
$this->i = $i;
array_push($this->inputs,$i);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。