如何将文件添加到Laravel Storage并将名称保存到DB
我可以逐个添加多个文件。我卡住了请帮帮我
这是我的View.blade.php
<div class="form-group">
{!! Form::label('name', 'Файлаа энд хуулна уу') !!}
{!! Form::file('attachments[]', ['roles' => 'form', 'multiple' => 'multiple']) !!}
{!! Form::token() !!}
</div>
{!! Form::submit('Submit', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
这是我的控制器
public function handleUpload(Request $request)
{
if (Auth::check()){
$files = $request->file('attachments');
$allowedFileTypes = config('app.allowedFileTypes');
$maxFileSize = config('app.maxFileSize');
$rules = ['file' => 'required|mimes:'.$allowedFileTypes.'|max:'.$maxFileSize];
$this->validate($request,$rules);
$fileName = now()->format('Y-m-d-H-i-s');
$userName = Auth::user()->name;
$division = $request->input('division');
$destinationPath = config('app.filesDestinationPath').'/'.$division.'/'.$userName.'/'.$fileName;
foreach ($files as $file) {
$uploaded = Storage::put($destinationPath,file_get_contents($file->getRealPath()));
}
if($uploaded){
Project::create([
'description' => $request->input('description'),
'division' => $request->input('division'),
'who' => $request->input('who'),
'whom' => $request->input('whom'),
'content' => $request->input('content'),
'filename' => $division.'/'.$userName.'/'.$fileName
]);
}
return redirect()->to('/projects/');
}
}
答案 0 :(得分:0)
您正在验证错误的输入参数。
stderr
更改为 -
$rules = ['file' => 'required|mimes:'.$allowedFileTypes.'|max:'.$maxFileSize];
我已根据您的代码进行了全面更改。试试这个 -
$rules = ['attachments.*' => 'required|mimes:'.$allowedFileTypes.'|max:'.$maxFileSize];