0

Hello All I am trying to get validate file in laravel with custom package but i give the error of the

"Method Illuminate\Http\UploadedFile::validate does not exist."

so please know what is a problem. MyController code:

$files=$request->file('image');
if($files){ 
    $oGreetr = new Greetr();
    return $oGreetr->file_size($files);
}

Package Code:

public function file_size($file){
    //return $file->getClientOriginalExtension();
    
    return $file->validate([
        'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);
}

Please help me to solve these error..

John Lobo
  • 14,355
  • 2
  • 10
  • 20

2 Answers2

0

I think issue is passing wrong param to package method. $request->file doesn't have validate method. So you have to do pass $request to package method

$files=$request->file('image');
if($files){ 
    $oGreetr = new Greetr();
    return $oGreetr->file_size($request);
}

and in package

public function file_size($request){
   
    
    return $request->validate([
        'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);
}
John Lobo
  • 14,355
  • 2
  • 10
  • 20
0

As $file variable does not have a validate function. You can you the following method to validate the custom variable. Also, use the Validator facade.

public function file_size($request){
    $validator = Validator::make($request->all(), [
            'image' => 'required',
    ]);
    return $validator;
  }
Bhagchandani
  • 548
  • 6
  • 21