Friday, April 14, 2017

Can't update a photo in laravel

Each user on my application has a cover photo, much like Facebook. I have been trying for days to be able to update it, but now I'm stuck. When I click update, the previous photo my user had gets erased, and the new photo does not appear, my default photo does.

Here's my controller

    public function updateCover(Request $request, $id)
{
    $user = User::findorFail($id)->with('cover_id');

    $input = $request->all();

    $input = Input::file('cover_id');
    $filename  = time() . '.' . $input->getClientOriginalExtension();
    $path = public_path('images' . $filename);
    Photo::make($input->getRealPath())->save($path);
    $user->cover = 'images'.$filename;
    $user->save();
}

And my view with my form

  {!! Form::model($user, ['method'=>'PUT', 'action'=>
   ['UserController@updateCover', $user->id], 'files'=>true]) !!}
   {!! Form::file('cover_id', ['class'=>'form-control']) !!}

I'm not an expert in laravel so for all I know I could be doing this completely wrong, I'm just trying to get the cover photo to change upon update.



via QuintonO14

Advertisement