title: Laravel 5.4 replace file if exist
body: <p>I need to solve a small inconvenience that I am presenting with the update of an image when modifying the associated registry.</p>
  
  <p>When I modify the registry I want to replace the associated file that is in the directory.</p>
  
  <p>This is my table structure:</p>
  
  <pre><code>Schema::create('institutions', function(Blueprint $table)
          {
              $table-&gt;engine = 'InnoDB';
              $table-&gt;increments('id');
              $table-&gt;string('name')-&gt;unique();
              $table-&gt;string('initials')-&gt;nullable();
              $table-&gt;string('description')-&gt;nullable();
              $table-&gt;string('avatar')-&gt;default('default.jpg');
              $table-&gt;timestamps();
          });
  </code></pre>
  
  <p>This is my update method on my controller:</p>
  
  <pre><code>public function update(Request $request, $id)
      {
          //
  
          $institution = $this-&gt;institution-&gt;find($id);
  
          try
          {
              $institution-&gt;update($request-&gt;all());
  
              if($request-&gt;hasFile('avatar')){
                  $avatar = $request-&gt;file('avatar');
                  $filename = time() . '.' . $avatar-&gt;getClientOriginalExtension();
                  Image::make($avatar)-&gt;resize(250, 205)-&gt;save( public_path('uploads/institutions/' . $filename ) );
  
                  $institution-&gt;avatar = $filename;
                  $institution-&gt;save();
              }
  
              $updated = $institution;
  
              $message = flash('Institución actualizada correctamente!!!', 'success');
  
              return redirect()-&gt;route('instituciones.index')-&gt;with('message', $message);    
          }       
  
          catch(\Illuminate\Database\QueryException $e)
          { 
              $message = flash('La institución no se actualizó correctamente!!!', 'danger');
  
              return redirect()-&gt;route('institutions.create')-&gt;with('message', $message); 
          }   
  
      }
  </code></pre>
  
  <p>I have tried different methods but I have not succeeded.</p>