<commit_before><?php
namespace Electro\Plugins\MatisseComponents\Traits;

use Illuminate\Database\Eloquent\Model;
use Electro\Plugins\MatisseComponents\Models\File;

trait FilesModelTrait
{
  /**
   * Get all the owned files.
   */
  public function files ()
  {
    /** @var Model $this */
    return $this->morphMany (File::class, 'owner');
  }

}<commit_msg>Improved FilesModelTrait: autmatically delete all files of a model being deleted.<commit_after><?php
namespace Electro\Plugins\MatisseComponents\Traits;

use Electro\Plugins\MatisseComponents\Models\File;
use Illuminate\Database\Eloquent\Model;

trait FilesModelTrait
{
  /**
   * Registers an event handler that, when a model is deleted, deletes all of its files.
   */
  public function bootFilesModelTrait ()
  {
    static::deleting (function ($model) {
      foreach ($model->files as $file)
        $file->delete ();
    });
  }

  /**
   * Get all owned files.
   */
  public function files ()
  {
    /** @var Model $this */
    return $this->morphMany (File::class, 'owner');
  }

}
{'fraction_non_alphanumeric': 0.12832699619771862, 'fraction_numerical': 0.0, 'mean_word_length': 3.461864406779661, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2640355', 'n_tokens_mistral': 336, 'n_tokens_neox': 335, 'n_words': 83}