我正在尝试提交一个表单,如果用户需要,可以选择上传多个文件。我不断收到错误“未定义的变量:数据”。我哪里做错了?
控制器
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|string|max:191',
'description' => 'required|string',
'date' => 'required|date|max:191',
]);
if ($request->hasfile('file')) {
foreach ($request->file('file') as $file) {
$fileName = $file->getClientOriginalName();
$file->move(public_path().'/files/', $fileName);
$data[] = $fileName;
}
}
return logbook::create([
'title' => $request['title'],
'description' => $request['description'],
'date' => $request['date'],
'file' => json_encode($data),
'student_id_FK' => session('userID'),
]);
}
富国沪深
浮云间