Search code examples
laravelvalidationstore

Laravel - can't store pdf file


I've got some problems for the file_path validation: so I decided to make the validation by myself (it's not the best and I wish I could fix it)

now, when I want to store the file, I've got an error:

Call to a member function storeAs() on string

I don't understand this error because my migration is on string, but on the web all the example did that too.

there are my migration:

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();
        });

my model:

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

my route:

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

and my model:

 /**
 * 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');
}

Solution

  • Because you use storeAs method on string, you should do that on your file input like this:

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

    third parameter is your filesystems disk and now your $path is:

    storage/app/public/YOURFILENAME