我想保存新数据时遇到问题我也不想上传文件,但我必须在我的公共函数存储中编写多个上传代码。我想在打开编辑表单并上传时添加文件。我应该怎么办?
我试图设置验证来设置但是当我提交错误未定义变量:数据时。
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.
一只名叫tom的猫