# OG images in Laravel

A trait on the model, a queued job, the URL in the database.

## Where it plugs in

In Laravel the queue is the natural home. The package adds a trait: saving a post dispatches a job, the job renders through the API and stores the URL, and a failed render retries itself. Until it is done, `<x-og-image :model="$post" />` prints a signed URL of the same image, so no page goes out without one.

`app/Models/Post.php`

```php
// composer require shareglint/laravel
// .env: SHAREGLINT_KEY, SHAREGLINT_ACCOUNT_ID, SHAREGLINT_SIGNING_SECRET

use Shareglint\Laravel\Concerns\HasOgImage;
use Shareglint\Laravel\Contracts\OgImageable;

final class Post extends Model implements OgImageable
{
    use HasOgImage;

    // On create and on every change, a queued job (after commit) renders
    // the image and stores its URL in the og_image column.
    public function ogImageFields(): array
    {
        return [
            'title' => $this->title,
            'author' => $this->author->name,
            'date' => $this->published_at,
            'reading_time' => $this->reading_minutes,
            'site' => config('app.domain'),
            'locale' => 'en',
        ];
    }

    // Re-render only when these change (default: any attribute).
    public function ogImageWatched(): ?array
    {
        return ['title', 'author_id', 'published_at'];
    }
}

// resources/views/posts/show.blade.php, in <head> - the stored URL,
// or a signed URL of the same image until the job has run:
// <x-og-image :model="$post" />
```

## The trap

Queue after the commit. The package dispatches with `afterCommit()`; if you write the job yourself and leave it out, the worker can start before the transaction that saved the post commits — and `findOrFail` blows up on a row that does not exist yet.

## What it costs

A hundred images a month free, then $9 for two thousand. The same post rendered twice returns the existing URL and uses no quota — so rebuilding your site costs nothing.
