我想保存新数据时遇到问题,我也不想上传文件,但是我必须在公共功能存储中编写多次上传的代码。我想在打开编辑表单并上传时添加文件。我该怎么办?
我尝试将验证设置为set,但是当我提交错误Undefined variable:data时。
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.
答案 0 :(得分:0)
您正在if中定义$ data变量,因此,如果条件为false,则不会定义$ 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();