title: Image update and remove old image on Laravel
body: <p>Trying to implement update article in my update controller it seems works, but the problem is when I only want to update the post without uploading an image the old always getting remove which is it shouldn't.</p>
  <p>here's my store function</p>
  <pre><code>public function store(Post $post)
  {
          $post-&gt;update($this-&gt;validateRequest());
            
          $this-&gt;storeImage($post);
          
  
          return redirect('post/'.$post-&gt;id)-&gt;with('success', 'New ariticle has been posted');
      }
  
  } 
  </code></pre>
  <p>here's my validation</p>
  <pre><code>private function validateRequest()
      {
          return request()-&gt;validate([
              'title'=&gt; 'required',
              'content' =&gt; 'required',
              'image' =&gt; 'sometimes|image|max:5000',
  
          ]);
      }
  </code></pre>
  <p>here's my update function</p>
  <pre><code>public function update(Post $post)
  {
          File::delete(public_path('storage/'.$post-&gt;image)); 
          $post-&gt;update($this-&gt;validateRequest());
            
          $this-&gt;storeImage($post);
          
  
          return redirect('post/'.$post-&gt;id)-&gt;with('success', 'This post has 
          been Edited');
      }
  
  } 
  </code></pre>
  <p>I've tried to add <code>File::delete</code> to my storeImage function and delete it from my update function, it fix the problem but the old image is not removed from directory</p>
  <pre><code>private function storeImage($post)
  {
    
    if (request()-&gt;has('image')){
    File::delete(public_path('storage/'.$post-&gt;image))
    $post-&gt;update([
         'image' =&gt; request()-&gt;image-&gt;store('uploads', 'public'),
    ]);
    
    $image = Image::make(public_path('storage/'.$post-&gt;image))-&gt;fit(750, 300);
    $image-&gt;save();
          }
      }
  </code></pre>
  <p>Ok since I use model binding in my controller I don't have to find the id right?
  so I change my update function which is basically Akhtar munir suggested, and turn out to be something like this. The image update work, it also remove the old image when I update it. But I have found another issue, the problem is when I edit article and title it didn't change like when I update it, I hope you can take look at this is this correct?</p>
  <pre><code>public function update(Post $post){
      $this-&gt;validateRequest();
      if(request()-&gt;hasFile('image') &amp;&amp; request('image') != ''){
              $imagePath = public_path('storage/'.$post-&gt;image);
              if(File::exists($imagePath)){
                  unlink($imagePath);
              }
              $image = request()-&gt;file('image')-&gt;store('uploads', 'public');
              $post-&gt;update([
                  'title' =&gt; request()-&gt;title,
                  'content' =&gt; request()-&gt;content,
                  'image' =&gt; $image,
              ]);
          }
  }
  </code></pre>