I would like to cleanup the locally seeded storage images when running php artisan migrate:fresh --seed
.
I noticed that all the database tables get dropped before the seeders even start. So my method for cleaning up local storage images was never touched:
private function cleanLocalFileSystem()
{
$files = File::all();
$files->each(function (File $file) {
$this->fileController->destroy($file->id); // removes associated files from storage.
});
}
So there must be a way to clear out theses images at the start of running the migrate:fresh --seed
command.
Right?
2
Answers
I ended up solving this issue myself before the solultion of @mamaye came in.
I made a
.bat
file in my Apache bin directory namedrefreshdb
. These are it's contents:Then I made the command with
php artisan make:command ClearStorage
.Now I can simply run the
refreshdb
command in the terminal and all is well.Again, this is simular to
mamaye
's solution but it worked for me.I suggest you create a custom artisan command on your project since Laravel may not have a direct command to handle it. So the idea is so allow you clean up before you migrate.
https://laravel.com/docs/11.x/artisan#registering-commands
Next, you write your logic for the created command. Go to app/Console/Commands/CleanupThenMigrate.php
**
Next, run
php artisan app:cleanup-then-migrate
I hope this helps.