Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media not available in the afterCreate lifecycle hook #14022

Open
slovenianGooner opened this issue Aug 22, 2024 · 0 comments
Open

Media not available in the afterCreate lifecycle hook #14022

slovenianGooner opened this issue Aug 22, 2024 · 0 comments
Labels

Comments

@slovenianGooner
Copy link
Contributor

slovenianGooner commented Aug 22, 2024

Package

filament/spatie-laravel-media-library-plugin

Package Version

v3.2.97

Laravel Version

v11.19.0

Livewire Version

v3.5.4

PHP Version

PHP 8.2.22

Problem description

I have a form that accepts a PDF file upload (handled by the plugin). It also accepts a thumbnail image, but in case that is null the app generates a thumbnail from the first page of the PDF.

I've tried to hook into the afterCreate lifecycle hook of the creation process to check for that. What happens is no media is available on the record, even though in the database row is added.

public function afterCreate(): void
{
        if (!$record->getFirstMedia('thumbnail')) {
            $pdf = $record->getFirstMedia('pdf');
            $thumbnailGenerator = app(GenerateThumbnailFromPdf::class);
            $record->addMedia($thumbnailGenerator->generate($pdf))->withResponsiveImages()->toMediaCollection('thumbnail');
        }
}

In this instance $pdf returns null. Note that I am not using database transactions.

Expected behavior

I would expect to already have media available here in this lifecycle hook.

Steps to reproduce

  1. Create a form with a SpatieMediaLibraryFileUpload component to upload the PDF (located
Forms\Components\SpatieMediaLibraryFileUpload::make('pdf')
                            ->label('PDF File')
                            ->collection('pdf')
  1. Hook into the lifecycle with afterCreate method:
public function afterCreate() {
     $pdf = $this->record->getFirstMedia('pdf');
    dd($pdf);
}

$pdf should be an instance of Media here.

Reproduction repository (issue will be closed if this is not valid)

https://github.com/slovenianGooner/filament-issue-demo

Relevant log output

No response

Donate 💰 to fund this issue

  • You can donate funding to this issue. We receive the money once the issue is completed & confirmed by you.
  • 100% of the funding will be distributed between the Filament core team to run all aspects of the project.
  • Thank you in advance for helping us make maintenance sustainable!
Fund with Polar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Todo
Development

No branches or pull requests

1 participant