Laravel - 无法存储 pdf 文件
Laravel - can't store pdf file
我在 file_path 验证中遇到了一些问题:所以我决定自己进行验证(这不是最好的,我希望我能解决它)
现在,当我要存储文件时,出现错误:
Call to a member function storeAs() on string
我不明白这个错误,因为我的迁移是在字符串上进行的,但是在网络上所有的例子都是这样做的。
有我的迁移:
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 \Illuminate\Http\Request $request
* @param \App\Models\Project $project
* @return \Illuminate\Http\Response
*/
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
我在 file_path 验证中遇到了一些问题:所以我决定自己进行验证(这不是最好的,我希望我能解决它)
现在,当我要存储文件时,出现错误:
Call to a member function storeAs() on string
我不明白这个错误,因为我的迁移是在字符串上进行的,但是在网络上所有的例子都是这样做的。
有我的迁移:
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 \Illuminate\Http\Request $request
* @param \App\Models\Project $project
* @return \Illuminate\Http\Response
*/
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