DEV Community

Cover image for Filament: Delete Attachments when Deleting a Record
Sergio Peris
Sergio Peris

Posted on • Edited on • Originally published at sertxu.dev

1

Filament: Delete Attachments when Deleting a Record

Filament allows you to add attachments to a record, but doesn’t delete them when you delete the record.

In order to solve this issue, we have two alternatives:

Listen to model’s deleting event

When a model it’s about to be deleted, it fires the deleting event. We can listen to this event to trigger the functionality responsible to delete any attachements before the model no longer exists.

Inside the model class we can add the booted method to register new event listeners to the model.

class Project extends Model
{
    protected $fillable = [
        'title', 'slug', 'repository', 'description', 'thumbnail',
    ];

    /**
     * The "booted" method of the model.
     */
    protected static function booted(): void
    {
        static::deleting(function ($project) {
            Storage::disk('public')->delete($project->thumbnail);
        });
    }
}
Enter fullscreen mode Exit fullscreen mode

This code will delete the thumbnail attachment before deleting the model.

You can read more about this in the Laravel documentation https://laravel.com/docs/11.x/eloquent#events-using-closures

Modify Filament’s delete action

Another option is to change the behaviour of the delete action.

protected function getActions(): array
{
    return [
        Actions\DeleteAction::make()
            ->after(function (Project $project) {
                // delete single
                if ($project->thumbnail) {
                    Storage::disk('public')->delete($project->thumbnail);
                }
            })
    ];
}
Enter fullscreen mode Exit fullscreen mode

You can use the option better fits your requirements, but you should keep in mind that adding an event listener will delete the attachment when your model is deleted, whenever has occured by a Filament action or another part of code in your app.

This is important because probably will determine whenever option you should choose.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay