Laravel CRUD编辑功能表单无法自动填充旧数据求助
Laravel资源路由编辑表单无法填充数据问题排查与解决
问题描述
使用Laravel资源路由实现CRUD时,编辑页面表单无法自动填充已有数据,无论是用value="{{ old('kodecust', $customer->kodecust ) }}"还是直接调用$customer->kodecust都无效。读取、删除、创建功能均正常,仅更新/编辑功能异常。
相关代码
路由
Route::resource('/customer/mastercustomer', CustomerController::class)->except(['destroy'])->middleware("auth"); Route::delete('/customer/mastercustomer/{customer:perusahaancust}', [CustomerController::class, 'destroy'])->middleware('auth');
控制器(edit和update方法)
public function edit(Customer $customer) { return view('Master\Customer\editcustomer', [ "customer" => $customer, "title" => "Edit Customer" ]); } public function update(Request $request, Customer $customer) { $validatedData = $request->validate([ 'kodecust' => 'required|string|max:15', 'perusahaancust' => 'required|string|max:255', 'kontakcust' => 'required|string|max:100', 'kotacust' => 'required|string|max:255', 'alamatcust' => 'required|string|max:255', 'alamat2cust' => 'nullable|string|max:255', 'notelponcust' => 'required|string|max:30', 'termcust' => 'required|integer', 'limitcust' => 'required', 'desccust' => 'nullable|string|max:50', ]); Customer::where('perusahaancust', $customer->perusahaancust) ->update($validatedData); return redirect('/customer/mastercustomer')->with('success', 'Berhasil Edit Customer!'); }
视图(部分表单代码)
<tr> <td><label for="kodecust">Kode</label></td> <td> <input type="text" name="kodecust" id="kodecust" value="{{ old('kodecust', $customer->kodecust ) }}" required style="width: 50px" @error('kodecust') class="is-invalid" @enderror> @error('kodecust') <div class="invalid-feedback">{{ $message }}</div> @enderror </td> <td><label for="perusahaancust">Perusahaan</label></td> <td> <input type="text" name="perusahaancust" id="perusahaancust" value="{{ old('perusahaancust', $customer->perusahaancust) }}" required style="width: 200px" @error('perusahaancust') class="is-invalid" @enderror> @error('perusahaancust') <div class="invalid-feedback">{{ $message }}</div> @enderror </td> </tr> <tr> <td><label for="kontakcust">Kontak</label></td> <td> <input type="text" name="kontakcust" id="kontakcust" value="{{ old('kontakcust', $customer->kontakcust) }}" required style="width: 100px" @error('kontakcust') class="is-invalid" @enderror> @error('kontakcust') <div class="invalid-feedback">{{ $message }}</div> @enderror </td> <td><label for="kotacust">Kota</label></td> <td> <input type="text" name="kotacust" id="kotacust" value="{{ old('kotacust', $customer->kotacust) }}" required style="width: 150px" @error('kotacust') class="is-invalid" @enderror> @error('kotacust') <div class="invalid-feedback">{{ $message }}</div> @enderror </td> </tr>
排查结果
在edit方法中使用dd($customer)输出的实例信息显示,模型实例未被正确加载:
App\Models\Customer {#1407 ▼ // app\Http\Controllers\CustomerController.php:73 #connection: null #table: "customers" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] +preventsLazyLoading: false #perPage: 15 +exists: false +wasRecentlyCreated: false #escapeWhenCastingToString: false #attributes: [] #original: [] #changes: [] #casts: [] #classCastCache: [] #attributeCastCache: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true +usesUniqueIds: false #hidden: [] #visible: [] #fillable: [] #guarded: array:1 [▼ 0 => "id" ] }
解决方案
问题根源是路由隐式绑定未正确匹配:删除路由指定了用perusahaancust作为路由键,但资源路由默认使用模型主键(id),导致edit/update方法无法正确加载模型实例。
步骤1:统一模型路由键
在Customer模型中设置默认路由键为perusahaancust,让所有资源路由统一使用该字段进行隐式绑定:
// app/Models/Customer.php class Customer extends Model { public function getRouteKeyName() { return 'perusahaancust'; } }
步骤2:简化路由定义
删除单独定义的delete路由,直接使用完整的资源路由:
Route::resource('/customer/mastercustomer', CustomerController::class)->middleware("auth");
步骤3:优化update方法
无需手动执行where查询,直接使用隐式绑定的模型实例更新数据:
public function update(Request $request, Customer $customer) { $validatedData = $request->validate([ // 原有验证规则保持不变 ]); $customer->update($validatedData); return redirect('/customer/mastercustomer')->with('success', 'Berhasil Edit Customer!'); }
步骤4:验证表单填充
完成上述修改后,edit方法中的$customer实例会被正确加载,表单输入框将自动填充对应数据,old()辅助函数也能正常处理验证失败后的回显。
内容的提问来源于stack exchange,提问作者NyK
相关产品推荐
相关产品推荐

