Is the avatar fillable on your User model?
Also you have broken syntax in the code you provided?
File:: // what's this supposed to be?
I am trying to upload an image using the tutorial here. I have followed the instructions exactly as in the video, but once I click on the submit button, it brings me back to the profile page, and the image is unchanged, nothing has changed in the database or uploaded to the folder designated. I am using Laravel 5.2.
Here is my routes.php:
Route::get('profile', 'UserController@showProfile');
Route::post('profile', 'UserController@updateAvatar');
Here is the blade file:
<div class="row">
<div class="col-sm-4">
<div class="text-align-center">
<img class="img-circle" src="/uploads/avatars/{{ Auth::user()->avatar }}" alt="64x64" style="height: 112px; border-radius:50%;">
</div>
<br>
<br>
<div class="text-align-center">
<form enctype="multipart/form-data" action="{{ url('/profile') }}" method="POST">
<h5>Update Profile Image</h5>
<input type="file" name="avatar">
<input type="hidden" name="_token" value="{{ csrf_token() }}"> <br>
<input type="submit" class="pull-left btn btn-sm btn-primary">
</form>
</div>
</div>
Here is the controller:
public function showProfile(){
return view('profile', array('user' => Auth::user()));
}
public function updateAvatar(Request $request){
//Handle the user upload of avatar
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(200, 200)->save( public_path('/uploads/avatars/' . $filename ) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('profile', array('user' => Auth::user()));
}
I have added a field avatar to the users table.
Please or to participate in this conversation.