[英]Laravel 8: using the same form for create & edit (not inserting some data)
可能的重復:
Laravel 8 使用相同的形式進行編輯和創建,
Laravel 使用相同的表單進行創建和編輯
我按照本指南重構並簡化了 controller
該表單適用於插入和編輯,但某些數據不會發送到DB
,例如:
ZoneNumber, ClaimingDate, receivedDate, lostItem
我沒有看到任何拼寫錯誤,也找不到任何阻止保存這些數據的東西。
我確保表單根據條件呈現:
@if($$lostFound->exists)
<form id="LostFoundForm" method="POST" action="{{ route('data-entry.lost-and-found.update', $lostFound) }}" enctype="multipart/form-data">
@method('put')
@else
<form id="LostFoundForm" method="POST" action="{{ route('data-entry.lost-and-found.store') }}" enctype="multipart/form-data">
@endif
@csrf
而且我在刀片中也有old()
助手,如下所示:
<div class="form-floating mb-10">
<input type="text" min="0" class="form-control @error('lostItem') is-invalid @enderror" name="lostItem" placeholder="lostItem" value="{{ old('lostItem', $lostFound->lostItem) }}">
<label for="lostItem">Item Name</label>
</div>
我的 controller 看起來不錯……我在這里看不到任何問題嗎?
public function create()
{
return $this->edit(new LostFound());
}
public function store(LostFoundRequest $request)
{
return $this->update($request, new LostFound());
}
public function edit(LostFound $lostFound)
{
$locationList = Locations::all();
return view('pages.dataEntry.lostFound._addForm', compact('lostFound', 'locationList'));
}
public function update(LostFoundRequest $request, LostFound $lostFound)
{
$request->persist($lostFound);
return redirect(route('data-entry.lost-and-found.index'))->with('success', 'LF updated successfully.');
}
persists persists()
function 用於保存數據:
public function persist(LostFound $lostFound)
{
// TODO: See if this can be refactored more.
return LostFound::create( $this->validated() )
->addMedia($this->LFImage)
->toMediaCollection('lost-Items');
$this->save();
}
是的,正如 Mash tan 和 EHF Shahab 在評論中所說。 ZoneNumber, ClaimingDate, receivedDate, lostItem
未添加到驗證數組中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.