All topics

How to Add Custom OG Images to Your Laravel App

Laravel does not have a built-in metadata management system, so OG images are typically handled through Blade template partials and a dedicated SEO package. The spatie/laravel-seo package is the most popular choice and integrates cleanly with Eloquent models. Here is how to set it up.

spatie/laravel-seo for model-level OG images

Install `spatie/laravel-seo` via Composer. The package adds a `HasSeoAttributes` trait to your Eloquent models and a fluent API for setting OG metadata. In your controller, call `seo()->setImage($post->og_image_url)` before returning the view. Add an `og_image_url` column to your posts table via a migration and populate it when content is created. The package handles injecting the meta tag into your Blade layout through a `{!! seo()->render() !!}` directive in your `<head>` section.

Blade partial for manual meta tag injection

For simpler setups without a package, create a `resources/views/partials/seo.blade.php` partial that renders OG tags from variables: `<meta property="og:image" content="{{ $ogImage ?? config('seo.default_og_image') }}">`. Include it in your main layout with `@include('partials.seo', ['ogImage' => $post->og_image_url ?? null])`. Store the default OG image URL in `config/seo.php` so it can be changed without touching templates. Pass the image URL from your controller to the view using `compact` or `with`.

Inertia.js: set meta tags in the page component head

If you are using Inertia.js with Vue or React, meta tags are set on the client side through the `<Head>` component that Inertia provides. In your Vue page component, use `<Head><meta property="og:image" :content="post.ogImageUrl" /></Head>`. Note that Inertia-rendered pages are single-page applications, which means social crawlers will only see the OG tag if your server renders the initial HTML with the correct meta tags. Combine Inertia with SSR (via Inertia's SSR adapter) or set the tags server-side through the initial page props in Laravel.

Add an og_image column and populate it on content creation

Add an `og_image_url` string column to your content tables. In the model's `boot` method or a dedicated observer, trigger OG image generation when a new post is saved. Store the returned CDN URL in the `og_image_url` column. This way the image is always ready by the time the page is publicly accessible, and your Blade template can render the URL without any async complexity. If an image is missing for a record, fall back to the site-wide default from your config file.

Generate your OG image in seconds

Paste a title, pick a brand color, and get production-ready social cards for every platform — with framework-specific meta tag snippets included.

Generate an OG image for your Laravel app