我想保存新数据时遇到问题我也不想上传文件,但我必须在我的公共函数存储中编写多个上传代码。我想在打开编辑表单并上传时添加文件。我应该怎么办?我试图设置验证来设置但是当我提交错误未定义变量:数据时。public function store(Request $request){ $request->validate([ 'employee_sid' => 'required', 'employee_npk' => 'required', 'employee_name' => 'required', 'file_uploads' => 'nullable', 'file_uploads.*' => 'file|image|mimes:pdf,jpg,jpeg,png', ]); if($request->file('file_uploads')) { foreach($request->file('file_uploads') as $file) { $name = $file->getClientOriginalName(); $path = 'public/file/'.$request->employee_name; $file->move($path, $name); $data[] = $name; } } $sid = new Sid; $sid->employee_sid = $request->employee_sid; $sid->employee_npk = $request->employee_npk; $sid->employee_name = $request->employee_name; $sid->file_uploads = json_encode($data); $sid->save();我希望当我在没有 file_uploads 的情况下提交该数据时。但错误Undefined variable: data.
1 回答
一只名叫tom的猫
TA贡献1906条经验 获得超3个赞
您正在 if 中定义 $data 变量,因此如果条件为假,则不会定义 $data 变量。一种可能的解决方法是:
$sid = new Sid;
$sid->employee_sid = $request->employee_sid;
$sid->employee_npk = $request->employee_npk;
$sid->employee_name = $request->employee_name;
$sid->file_uploads = isset($data)? json_encode($data): null;
$sid->save();
- 1 回答
- 0 关注
- 117 浏览
添加回答
举报
0/150
提交
取消