Laravel共用创建编辑表单报$data is undefined错误修复方法
问题描述
复用同一套Blade表单承载自动扣款计划的新增、编辑两类场景时,编辑场景通过调用$data等变量实现数据回显,但打开新增表单页面时会触发 $data is undefined 报错。
相关路由定义:
Route::resource('/tabungan/jadwal-autodebet', SavingAccountScheduleController::class);
问题根因
- 控制器
create方法未向视图传递编辑场景使用的$data、$origin、$destination、$destinationType变量,但视图中大量位置直接读取这些变量的属性,新增场景下变量不存在直接触发报错。 edit方法视图传参格式不规范,将视图数据拆分为两个独立参数传入view()方法,存在变量传递失败的兼容风险。- 视图中回显逻辑未做场景判断,新增场景下仍强制读取编辑数据的属性值。
修复步骤
1. 修正控制器传参逻辑
在create方法中为编辑场景专属变量赋默认空值,同时修正edit方法的传参格式,将所有视图变量合并为同一个数组传递,移除冗余的clone操作,用findOrFail做数据不存在的异常兜底。
修正后的控制器代码:
public function create() { $info['header'] = $this->title; $info['description'] = 'Tambah'; $info['breadcrumb'] = null; // 为编辑场景变量赋默认空值,避免未定义报错 $data = null; $origin = null; $destination = null; $destinationType = ''; return view('banking::autodebet-schedule.form', array_merge($info, compact('data','origin','destination','destinationType'))); } public function edit($id) { $info['header'] = $this->title; $info['description'] = 'Ubah'; $info['breadcrumb'] = null; // 用findOrFail自动处理数据不存在的404场景 $data = TransactionSchedule::findOrFail($id); // 移除冗余的clone操作 $origin = $data->origin; $destination = $data->destination; $destinationType = ''; $saving = SavingAccount::class; $loan = LoanAccount::class; if($data->origin_type == $saving){ $destinationType = 'Tabungan'; }elseif($data->origin_type == $loan){ $destinationType = 'Pinjaman'; } // 所有视图变量合并为单个数组传递 return view('banking::autodebet-schedule.form', array_merge($info, compact('data','origin','destination','destinationType'))); }
2. 修正视图变量调用逻辑
所有涉及编辑数据回显的位置,增加存在性判断,仅在编辑场景下输出回显内容,新增场景输出默认空值/默认选项。
关键位置修正示例:
- 目标产品下拉框:
<select name="destination_type" id="destination_type" class="select2 form-control destination_type"> @if(isset($data) && $data) <option value="{{ $data->origin_type }}" selected>{{ $destinationType }}</option> @endif <option class="type_option" id="SavingAccount" value="Modules\Banking\Entities\SavingAccount">Tabungan</option> <option class="type_option" id="LoanAccount" value="Modules\Banking\Entities\LoanAccount">Pinjaman</option> </select>
- 源账号、目标账号下拉框:
<select id="origin_code" class="select2 form-control origin_code"> @if(isset($origin) && $origin) <option value="{{ $origin->id }}" selected>{{ $origin->saving_code }}-{{ $origin->customer->customer_name}}-{{ $destinationType }}</option> @endif </select> <select id="destination_code" class="select2 form-control destination_code" > @if(isset($destination) && $destination) <option value="{{ $destination->id }}" selected>{{ $destination->saving_code }}-{{ $destination->customer->customer_name}}-{{ $destinationType }}</option> @endif </select>
- 扣款日期下拉框:
<select name="scheduled_day" id="scheduled_day" class="select2 form-control scheduled_day"> @if(isset($data) && $data) <option disabled selected value="{{ $data->scheduled_day }}">{{ $data->scheduled_day }}</option> @endif @for ($i = 1; $i <= 31; $i++) <option class="day_option" id="day_{{ $i }}" value="{{ $i }}">{{ $i }}</option> @endfor </select>
- 备注文本域:
<textarea name="desc" class="form-control desc" id="" cols="30" rows="5">{{ $data->desc ?? '' }}</textarea>
额外优化提示
- 下拉框回显选项需要加
selected属性,否则编辑场景下回显值不会被默认选中。 - 对于可能为空的变量读取,统一使用
??空合并运算符设置默认值,可进一步降低未定义报错的概率。
内容的提问来源于stack exchange,提问作者Ochii
相关产品推荐
相关产品推荐

