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

getFirstMediaUrl returns null in laravel resource observer created method #438

Open
fredchess opened this issue Apr 1, 2024 · 0 comments

Comments

@fredchess
Copy link

Hello, I'm using Images field to attach an image to a resource. After the resource created I want to created associated product in Stripe.
To do this I have created an resource Observer but when I want to retrieve image from resource using getFirstMediaUrl(), this returns null.

This is my observer:

class FormuleObserver implements ShouldHandleEventsAfterCommit
{
public function __construct(public StripeClient $stripeClient)
{

}

public function saved(Formule $formule): void
{
    try {
        $product = $this->stripeClient->products->create(
            [
                'name' => $formule->name,
                'description' => $formule->mini_description,
                'default_price_data' => [
                    'currency' => 'EUR',
                    'unit_amount' => $formule->price * 100,
                ],
                'images' => [
                    $formule->getFirstMediaUrl('formule_images')
                ]
            ]
        );

        $formule->update([
            'stripe_product_id' => $product['id']
        ]);
    } catch (Exception $e) {
        error_log($e->getMessage());
        error_log($formule->getFirstMediaUrl('formule_images'));
        $formule->delete();
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant