拉拉维尔 - 无法存储 pdf 文件



我在文件路径验证方面遇到了一些问题:所以我决定自己进行验证(这不是最好的,我希望我能解决它(

现在,当我想存储文件时,我遇到了一个错误:

Call to a member function storeAs() on string

我不理解这个错误,因为我的迁移是在字符串上进行的,但在web上,所有的例子都是这样做的。

有我的迁移:

Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('job')->nullable();
$table->longText('presentation')->nullable();
$table->string('file_path')->nullable();
$table->rememberToken();
$table->timestamps();
});

我的型号:

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
'job',
'presentation',
'file_path',
];

我的路线:

Route::get('user', [UserController::class, 'index'])->name('user.index'); //INDEX
Route::patch('user/{auth}', [UserController::class, 'update'])->name('user.update'); //UPDATE

和我的型号:

/**
* Update the specified resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @param  AppModelsProject  $project
* @return IlluminateHttpResponse
*/
public function update(Request $request, $user)
{   
$user = auth()->user();

$request->validate([
'name' => 'string|required|max:255',
'email' => 'email|required',
'password' => 'string|nullable',
'job' => 'string|required|max:255',
'presentation' => 'string|required|max:25000',
// 'file_path' => 'nullable|mimes:pdf',
]);

// $hashedPassword = Hash::make($request->password);

if ($request->filled('file_path')) {
$extension = explode(".", $request->file_path);

if ($extension[1] == "pdf"){
storage::delete($user->file_path);
$filePath = $request->file_path->storeAs('/storage', 'SchneiderBart.pdf');
$request->file_path->move(public_path('/storage'), 'SchneiderBart.pdf');
$user = User::find($user);
$user->name = $request->name;
$user->email = $request->email;
$user->job = $request->job;
$user->presentation = $request->presentation;
$user->file_path = $filePath;
$user->save();    
}else{
return Redirect::back()->withErrors(['The file must be a pdf']);
}  

}else{

$user = User::find($user);
$user->name = $request->name;
$user->email = $request->email;
$user->job = $request->job;
$user->presentation = $request->presentation;
$user->save();
}

return view('admin');
}

因为在字符串上使用storeAs方法,所以应该在文件输入上这样做:

$file = $request->file('file_path');
$path = $file->storeAs('/', $name, 'public');
$user->file_path = $path;

第三个参数是您的文件系统磁盘,现在您的$path是:

storage/app/public/YOURFILENAME

最新更新