Models and the Trait
Instead of listing columns in sources, you can declare them on the model with the HasStorageFiles trait.
use Erag\OrphanFiles\Concerns\HasStorageFiles;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasStorageFiles;
protected array $storageFiles = [
'cover' => 'path',
'gallery' => ['type' => 'json', 'key' => '*.path'],
'body' => 'html',
];
protected ?string $storageDisk = 'public';
}Each entry in $storageFiles becomes a source on the model's table. The value is either a type ('path') or an array with the same options as a config source, such as type, key, disk or separator.
$storageDisk is the default disk for every column on the model. A column can override it with its own disk. Leave both out and the column matches paths on every scanned disk.
The trait only declares columns
The trait doesn't listen to model events. Saving, updating or deleting a Post never touches storage. Files are only checked when you run scan, clean or explain, and only removed by orphan:clean --force.
Model discovery
'discovery' => [
'enabled' => true,
'paths' => [
app_path('Models'),
],
'models' => [],
],Every class in paths that extends Model, isn't abstract and uses HasStorageFiles is picked up automatically. If some models live elsewhere, for example in a Domain folder, list them in models:
'models' => [
App\Domain\Blog\Post::class,
],Set enabled to false to stop scanning folders. Models in models are still used.
You can mix both approaches. A column that is declared on a model and also listed in sources is simply read twice.