Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined variable: request

Tags:

laravel

When I tried to upload the file it shows error:

Undefined variable: request

This is where I used it: UploadController:

if($request->hasFile('file')){
        $file = $request ->file('file');
        $fileName = $file->getClientOriginalName();
        $destinationPath = config('app.fileDesinationPath').'/'.$fileName;
        $uploads = Storage::put($destinationPath,file_get_contents($file->getRealPath()));

    }
    return redirect()->to('/upload');

What's wrong here?

like image 454
Rock Avatar asked Jan 13 '17 08:01

Rock


2 Answers

Add Request $request parameter in your function. Example:

public function yourFunction(Request $request)
{
    if($request->hasFile('file')){
        $file = $request ->file('file');
        $fileName = $file->getClientOriginalName();
        $destinationPath = config('app.fileDesinationPath').'/'.$fileName;
        $uploads = Storage::put($destinationPath,file_get_contents($file->getRealPath()));

    }
    return redirect()->to('/upload');
}

Please read the documentation thoroughly: http://laravel.com/docs

You can also watch Laravel tutorials here: http://laracasts.com

like image 132
aceraven777 Avatar answered Nov 09 '22 23:11

aceraven777


You can also use request() helper function as:

if(request()->hasFile('file')) {
    ...
}

The request function returns the current request instance.

like image 45
Amit Gupta Avatar answered Nov 09 '22 23:11

Amit Gupta