October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk6 min

How to Add Open Graph Tags to a Laravel Website

Put Open Graph tags in a shared Laravel Blade layout and supply escaped, page-specific values from views or controllers. Here’s how to implement and check them.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add Open Graph tags to the shared Blade layout inside its <head>, then provide page-specific titles, descriptions, canonical URLs, and images from each view or controller. Use Blade’s escaped output for dynamic values, and inspect the rendered HTML to verify the tags are present.

Which Open Graph tags should a Laravel page include?

The Open Graph protocol’s four basic properties are og:title, og:type, og:image, and og:url. Add og:description for a useful summary, and og:site_name when appropriate. The Open Graph protocol describes the fields and their meaning.

As an Amazon Associate I earn from qualifying purchases.

  • og:title: the title to represent the page.
  • og:type: the object type, such as website or article.
  • og:url: the canonical URL identifying that page.
  • og:image: the URL of the image representing it.
  • og:description: a concise description.
  • og:image:alt: a description of the image, not a caption. If you specify og:image, the protocol says to specify this field as well.

Use public, absolute URLs for the page and image so a remote preview crawler can request them. This is practical guidance for URL-valued metadata; the protocol does not prescribe a Laravel-specific hosting setup.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add the tags to a shared Blade layout

Laravel views commonly live under resources/views. In a typical application, place the tags in the shared layout—for example, resources/views/layouts/app.blade.php—so each page using that layout emits metadata in its document head.

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <title>@yield('title', config('app.name'))</title>

    <meta property="og:title" content="@yield('og_title', config('app.name'))">
    <meta property="og:type" content="@yield('og_type', 'website')">
    <meta property="og:url" content="@yield('og_url', url()->current())">
    <meta property="og:description" content="@yield('og_description', '')">
    <meta property="og:image" content="@yield('og_image', asset('images/social-default.jpg'))">
    <meta property="og:image:alt" content="@yield('og_image_alt', '')">
</head>
<body>
    @yield('content')
</body>
</html>

Replace the example default image path with an image that exists and is publicly reachable. The fallback title and type are useful defaults, but page-specific values should override them for content pages.

Set metadata for each page

A child Blade view can supply sections for the layout. This example assumes the route is named posts.show and the post has the referenced fields; adapt those names to your application.

@extends('layouts.app')

@section('title', $post->title)
@section('og_title', $post->social_title ?: $post->title)
@section('og_type', 'article')
@section('og_url', route('posts.show', $post))
@section('og_description', $post->meta_description ?: Str::limit(strip_tags($post->body), 200))
@section('og_image', $post->social_image_url)
@section('og_image_alt', $post->social_image_alt ?: $post->title)

@section('content')
    <article>
        <h1>{{ $post->title }}</h1>
        {!! $post->body !!}
    </article>
@endsection

The og:url should identify the canonical page URL, rather than a temporary or tracking URL. Confirm the route name and generated URL are correct for the deployed site.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pass values from a controller instead

If you prefer to assemble page metadata in a controller, Laravel’s view helper accepts data alongside the view name:

public function show(Post $post)
{
    return view('posts.show', [
        'post' => $post,
        'ogTitle' => $post->social_title ?: $post->title,
        'ogDescription' => $post->meta_description,
        'ogUrl' => route('posts.show', $post),
        'ogImage' => $post->social_image_url,
        'ogImageAlt' => $post->social_image_alt ?: $post->title,
    ]);
}

Then have the view expose those values to the shared layout, either through sections or an include. See Laravel’s documentation for views and passing data to views and Blade templates.

Keep dynamic metadata safely escaped

Use Blade’s escaped {{ }} interpolation for titles, descriptions, image URLs, and alt text. Laravel documents that these echo statements are passed through PHP’s htmlspecialchars function to help prevent cross-site scripting (XSS). Avoid {!! !!} for metadata or other user-provided values. The body example uses unescaped output only as a separate content-rendering choice; sanitize user-entered HTML according to your application’s requirements before rendering it that way.

Include useful image metadata

The protocol supports additional image properties: og:image:secure_url, og:image:type, og:image:width, og:image:height, and og:image:alt. Add details that are accurate for the image you serve. If you emit multiple og:image values, the protocol prefers the first in case of conflicting values; put structured image fields after the corresponding image tag.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Other optional properties include og:determiner, og:locale, and og:site_name. Do not add values just to fill out the head: choose those that accurately describe the page.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When Laravel Head may be a better fit

For a small site, direct Blade markup is straightforward and keeps the emitted HTML visible in the layout. If you need shared defaults, page-specific runtime metadata, field-level precedence, or integrations for Blade, Livewire, and Inertia, Laravel’s Laravel Head documentation describes a structured package approach. Check its guidance against your installed Laravel release before adopting it.

Install and define defaults

composer require laravel/head

A typical setup defines defaults in a service provider, then sets metadata while loading an individual page:

use LaravelHeadEnumsOgType;
use LaravelHeadFacadesHead;
use LaravelHeadHeadBuilder;

// In a service provider boot method:
Head::defaults(fn (HeadBuilder $head) => $head
    ->title(config('app.name'))
    ->description('')
    ->og(siteName: config('app.name'), type: OgType::Website));

// In a controller action for one post:
Head::title($post->title)
    ->description($post->meta_description)
    ->og(type: OgType::Article, title: $post->title)
    ->ogImage($post->social_image_url, alt: $post->social_image_alt ?: $post->title);

return view('posts.show', ['post' => $post]);

Render the resolved metadata in the shared layout:

<head>
    <meta charset="utf-8">
    @head
</head>

Laravel Head resolves defaults, route metadata, runtime metadata, and error metadata in precedence order, replacing lower-level values field by field. Its @head directive renders synchronously, so define the page metadata before the layout is rendered.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Verify the rendered page, not just the template

  1. Open a representative page in a browser and inspect its document source or response HTML.
  2. Check that all expected og: tags appear inside <head>, not only in client-side code that may not be present in the server response.
  3. Confirm the page-specific title, type, canonical URL, description, image URL, and image alt text are correct.
  4. Check that the referenced image and page URL are publicly reachable and that the image URL points to the intended asset.
  5. Use the current preview or debugging tool for the particular social platform if you need to check what it has fetched. Presence in the HTML does not establish that a platform has fetched or refreshed the metadata.

Common implementation problems

  • The shared layout shows defaults on every page: confirm the child view extends the expected layout and defines the metadata sections, or that the controller’s metadata setup runs before rendering.
  • The preview has no image: check that the rendered og:image is an absolute, publicly accessible URL and that og:image:alt is also emitted. A tag in a template is not proof that a particular platform fetched the image.
  • The URL points to the wrong page: generate it from the intended canonical route and inspect the actual response. Avoid relying on a current request URL if it can include query parameters or otherwise differ from the canonical address.
  • Metadata contains unexpected characters or markup: use escaped Blade output and keep HTML out of metadata values.
  • Laravel Head tags are missing or stale: because @head renders synchronously, define the metadata before the layout is rendered and check the package API against your Laravel version.
  • A platform still shows an old or different preview: the HTML check confirms only what your server returns. Platform cache expiry and debugger behavior vary and are not established here; consult the current tooling for the target platform.

Or skip the browser setup

You can inspect a page’s rendered screenshot with ScreenshotNeo, a website screenshot API and MCP server for developers. It does not replace checking the HTML source when you need to verify exact metadata, but it can help review how the page renders.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com/posts/example -o shot.webp

See the ScreenshotNeo documentation for API details. ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The Free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000.

Sign up for 1,000 free screenshots a month, with no card required.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.