无法使用Form Facade在Laravel 5.2中上传图像

问题描述:

I have been struggling to upload an image using a simple form. Every time I upload the file, it says there is no file, as per the functionality I've set up in the controller. Here is the code so far:

The blade form:

 <div class="text-align-center">
    {{Form::open(['url' => 'profile', 'files' => true]) }}
    {{Form::file('avatar')}}
    {{Form::submit('Update',['class' => 'pull-left btn btn-sm btn-primary']) }}
    {{Form::close() }}
</div>

The routes file:

Route::get('profile', 'UserController@profile');
Route::post('profile', 'UserController@avatar');
Route::resource('users','UserController');

The UserController:

public function profile(){

            return view('profile', array('user' => Auth::user()));
    }   

    public function avatar(Request $request){

        if($request->hasFile('avatar')){
                var_dump($request);
        }
        else
                die('there is no file here!');
}

The User model:

protected $fillable = [
        'username', 'email', 'avatar'
    ];

My php.ini has maximum file size limits of 8M, the file I'm trying to upload is only a few KB.

I had not cleared the views cache in a long while. This goes to show that the cache could impede new forms from being added. It was still loading the view with two forms, one of which I had already deleted. Once I cleared the view cache with view:clear, and then made a new view with the same code and file name, it was all working then.