fmaglia's avatar

BadMethodCallException in FileUpload example

Hi guys, I'm a newbie of Laravel and I'm trying to implement a file upload script. Unfortunately, when I submit a PDF file I obtained a BadMethodCallException. How I can solve the problem? Thanks in advance.

0 likes
14 replies
fmaglia's avatar

tutsmake dot com website, article: laravel-8-file-upload-tutorial

bugsysha's avatar

You can solve it by reading what the exception says. It always points which method doesn't exist.

fmaglia's avatar

Method Illuminate Validation Validator validatePdf does not exist.

bugsysha's avatar

It says it all there. validatePdf method doesn't exist.

fmaglia's avatar

I know it, but I don't have validatePdf method in my code. In my store() function I have the following code

public function store(Request $request)
{
    // echo $request;
    $validatedData = $request->validate([
     'file' => 'required|pdf|max:2048',

    ]);

    // echo $validatedData;

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

    $path = $request->file('file')->store('public/files');


    $save = new File;

    $save->name = $name;
    $save->path = $path;

    return redirect('file-upload')->with('status', 'File Has been uploaded successfully in laravel 8');

}
bugsysha's avatar
bugsysha
Best Answer
Level 61

Try:

'file' => 'required|mimes:pdf|max:2048',
MichalOravec's avatar

Why people don't read a documentation...

It's so simple.

fmaglia's avatar

Thanks, it seems working. Now, another problem occurs. The upload doesn't make errors, but the file isn't moved to the folder and no updates in the status ("File Has been uploaded successfully in laravel 8"). Have u any idea?

bugsysha's avatar

@fmaglia please close this discussion and open a new one. Thanks.

@michaloravec I agree, the best thing developer can learn is to read error logs, debug backtrace and read the docs.

1 like
MichalOravec's avatar

The problem will be with validation

This is correct

'file' => 'required|csv,txt,xlx,xls,pdf|max:2048',

And this is wrong

'file' => 'required|csv,txt,xlx,xls|pdf|max:2048',

pdf valdiation rule doesn't exist.

1 like

Please or to participate in this conversation.