title: How to perform an action based on Eloquent 'deleting' event when executing a mass delete statement in Laravel 5.4?
body: <p>There are 2 Eloquent models in my project: <code>App\StoredFile</code> and <code>App\StoredImageSize</code>.</p>
  
  <p>The <code>StoredFile</code> model is responsible for keeping uploaded files info. If the uploaded file would be an image, then 2 thumbnails will be stored in storage and their attributes in DB (<code>StoredImageSize</code> model).</p>
  
  <p>Relationships are as below:</p>
  
  <p><strong>StoredFile.php:</strong></p>
  
  <pre><code>public function sizes()
  {
      return $this-&gt;hasMany('App\StoredImageSize');
  }
  </code></pre>
  
  <p><strong>StoredImageSize.php:</strong></p>
  
  <pre><code>public function originalImage()
  {
      return $this-&gt;belongTo('App\StoredFile');
  }
  </code></pre>
  
  <p><code>StoredImageSize</code> has an observer class (<code>StoredImageSizeObserver</code>)  which  has been registered through a ServiceProvider and works well.</p>
  
  <p>The problem is that I want to delete the actual file from storage (in my case hard disk) when deleting the subsequent row from database.</p>
  
  <p><strong>StoredImageSizeObserver.php:</strong></p>
  
  <pre><code>public function deleting(StoredImageSize $file)
  {
      Storage::delete($file-&gt;server_url);
  }
  </code></pre>
  
  <p>The logic is something like this:</p>
  
  <pre><code>$file = StoredFile::find(1);
  $file-&gt;sizes()-&gt;delete();    // it does delete all related rows from 'StoredImageSize' but not files from storage
  $file-&gt;delete()
  </code></pre>
  
  <p>actually it never hits <code>deleting</code> method from <code>StoredImageSizeObserver</code> class as <a href="https://laravel.com/docs/5.4/eloquent#deleting-models" rel="noreferrer">Laravel documentation</a> says: </p>
  
  <blockquote>
    <p><em>When executing a mass delete statement via Eloquent, the deleting and deleted model events will not be fired for the deleted models. This is because the models are never actually retrieved when executing the delete statement.</em></p>
  </blockquote>
  
  <p>So, How should I tackle this situation? Any solution or recommendation would be appreciated.</p>