spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.78k stars 1.08k forks source link

addMediaFromDisk Method not defined when used in Queue Job #3555

Closed gepopp closed 8 months ago

gepopp commented 8 months ago

Hi there, im using the latest versions of laravel and livewire.

I have an existing Image model and i want to move the images to the media library, so i wanted to dispatch a job foreach existing image like so:

class MoveReleaseImage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     */
    public function __construct( public Image $image )
    {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        if(Storage::disk('s3')->exists($this->image->path) && Storage::disk('s3')->size($this->image->path) < 1024 * 1024 * 30){
            $this->image->addMediaFromDisk( $this->image->path, 's3' )
                    ->withResponsiveImages()
                    ->withCustomProperties( [
                        'alt'     => $this->image->alt,
                        'caption' => $this->image->description,
                    ] )->toMediaCollection('images', 'ir');
        }
    }
}

If i dispatchSync that job everthing works as expected but if i dispatch it on the queue i get an Method addMediaNotFound err. The image model implemnts the HasMedia and uses the InteractsWithMedia trait:

class Image extends Model implements HasMedia
{

    use HasFactory, InteractsWithMedia;

    protected $guarded = [];

    protected $appends = [ 'path' ];

...

Is there any restirction if the app runs from the console?