【问题标题】:Laravel - can't store pdf fileLaravel - 无法存储pdf文件
【发布时间】:2021-06-17 16:15:52
【问题描述】:

我在 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');
}

【问题讨论】:

  • 您的表单中有文件输入吗?
  • 是的,我使用带有补丁方法和 csrf 的路由 user.update。这是我的输入<input type="file" name="file_path" id="file_path" value="{{$user->file_path}}">

标签: laravel validation store


【解决方案1】:

因为你在字符串上使用了 storeAs 方法,你应该像这样在你的文件输入上这样做:

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

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

storage/app/public/YOURFILENAME

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    相关资源
    最近更新 更多