emmexx's avatar
Level 1

Get related model in static::saved event

I have a model for images that is related to 2 different objects/models, say Article and Product. I use Filament to upload article and product data and the related images, I created ArticleResource and ProductResource, both have a form with a relationship to Image. When an image is uploaded I create 4 other images from it. I do it in the booted() method of the Image model:

   protected static function booted()
    {
        static::saved(function (Image $image) {
            if($image->isDirty('path'))
            {
                self::transform($image->path); //create 4 images from $image->path
            }
        });
    }

At the moment I'm doing it only for Article but I'd like to be able to save the 4 images in different folders if the image is uploaded using ArticleResource or ProductResource. Is it possible to know where the model ($image) is coming from inside the static::saved event? Or is there any other (better) way to accomplish that?

Thank you

maxx

0 likes
0 replies

Please or to participate in this conversation.