【发布时间】:2021-04-22 21:33:11
【问题描述】:
我是 Livewire/Jetstream 的新手,我正在尝试制作一个小库存应用程序来尝试一下。在下面的示例中,我试图在表格中显示数据库中的库存项目,并能够在不进入编辑页面的情况下更新表格中的库存名称和数量。
我有一个嵌套的 foreach,当我呈现页面时,循环中的输入字段会显示该值,然后消失,但该值在 HTML 中正确显示。任何帮助将不胜感激!
**Show Inventory**
namespace App\Http\Livewire;
use App\Models\Inventory;
use Livewire\Component;
class ShowInventory extends Component
{
public $inventories;
public function mount()
{
$this->inventories = Inventory::orderBy('name')->get();
}
public function render()
{
return view('livewire.show-inventory');
}
public function name()
{
$this->name = $name;
}
public function update($id)
{
$data = $this->validate([
'name' => 'required',
'available_on_hand' => 'required',
]);
$this->item_id = $id;
$item = Inventory::find($this->item_id);
$item->update([
'name' => $this->name,
'available_on_hand' => $this->available_on_hand,
]);
}
}
**Show Item**
namespace App\Http\Livewire;
use App\Models\Inventory;
use Livewire\Component;
class ShowItem extends Component
{
public $inventory;
public function mount(Inventory $inventory)
{
$this->inventory = $inventory;
}
public function render()
{
return view('livewire.show-item');
}
}
**Parent Blade**
<table class="table-auto">
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th></th>
<th>View</th>
</tr>
</thead>
<tbody>
@foreach($inventories as $inventory)
@livewire('show-item', ['inventory' => $inventory], key($inventory->id))
@endforeach
</tbody>
</table>
**Nested Blade**
<form wire:submit.prevent="update({{ $inventory->id }})">
<tr>
<td><input type="input" wire:model="name" value="{{$inventory->name}}" /></td>
<td><input type="input" wire:model="available_on_hand" value="{{$inventory->available_on_hand}}" /></td>
<td><button type="submit">Save</button></td>
<td>View</td>
</tr>
</form>
【问题讨论】:
-
如果你从前端绑定属性不需要值属性
-
我正在添加 value 属性以显示当前值,并可选择在表格中内联编辑它们。
标签: php laravel laravel-livewire