存储文件时出现Laravel错误



我正在进行一个laravel crud项目。现在我想存储像.xlsx和.docx这样的文件但我的控制器和浏览器中不断出现错误:

控制器:

public function store(Request $request)
{
$request->validate([
'title'=>'required',
'description_short'=>'',
'description_long'=>'',
'file'=>'',
'language_id'=> [
'required', 'exists:language,id'
],
]);
$fileName = $request->file->getClientOriginalName();
$filePath = 'files/' . $fileName;
$path = Storage::disk('public')->put($filePath, file_get_contents($request->file));
$path = Storage::disk('public')->url($path);
$file = new File([
'title'=> $request->get('title'),
'description_short'=> $request->get('description_short'),
'description_long'=> $request->get('description_long'),
'file'=>$request->get('file'),
'language_id'=> $request->language_id,
]);
$file->save();

return back();
}

这里我得到了错误:未定义的方法"url">

创建页面:

<form method="post" action="{{ route('admin.language.store') }}" enctype="multipart/form-data">
@csrf
<div class="form-group">    
<label for="title">{{('name')}}</label>
<input type="text" class="form-control" name="name"/>
</div>
<div class="form-group">
<label for="value">{{('file')}}</label>
<input type="file" class="form-control" name="file"/>
</div>        

<button type="submit" class="btn btn-primary">Add language</button>
</form>

我得到的浏览器错误是:对字符串调用成员函数getClientOriginalName((。

如果我需要提供更多的信息,我会很乐意的!

fileRequest class中的保留关键字,用于在post方法中获取提交的文件。

您不能在输入中使用file。所以,首先您必须更改输入框中的文件名。

之后你可以做下面的事情。

$request->file('file_input_name')->getClientOriginalName();

$file = $request->file->getClientOriginalName();

修复

最新更新