# Laravel

## Getting Started

## Create a connection

1. Sign in to [OpenGraph+](/)
2. Go to your website's **Meta Tags** page
3. On your first visit you'll see the Terms of Service; click **Agree & Create Connection**, then copy your connection URL

Your connection URL looks like `https://$OGPLUS_KEY.ogplus.net`.

## Find your layout

Laravel layouts live in `resources/views/layouts/`. Most apps use `app.blade.php` as the base layout. Blade views extend layouts with `@extends('layouts.app')`.

## Add Open Graph meta tags

Add the following meta tags to your layout's `<head>`. These control how your pages appear when shared on Twitter, Slack, LinkedIn, and other platforms.

```html
<!-- resources/views/layouts/app.blade.php -->
<html>
  <head>
    <meta property="og:title" content="@yield('og_title', 'My Site')">
    <meta property="og:description" content="@yield('og_description', 'Welcome to my site')">
    <meta property="og:url" content="{{ url()->current() }}">
    <meta property="og:site_name" content="My Site">
    <meta property="og:type" content="@yield('og_type', 'website')">
    <meta property="og:image" content="https://$OGPLUS_KEY.ogplus.net/{{ request()->path() }}">
    <meta name="twitter:card" content="summary_large_image">
  </head>
  <body>
    @yield('content')
  </body>
</html>
```

Replace `https://$OGPLUS_KEY.ogplus.net` with your connection URL, and `"My Site"` with your actual site name.

**Note:** `request()->path()` returns the path without a leading slash (e.g., `posts/1` not `/posts/1`), so include the `/` after the connection URL domain.

### What each tag does

| Tag | Purpose |
|-----|---------|
| `og:title` | The title shown in the link preview |
| `og:description` | The description shown below the title |
| `og:url` | The canonical URL of the page |
| `og:site_name` | Your website's name |
| `og:type` | Content type (`website`, `article`, `product`) |
| `og:image` | The social card image (powered by OpenGraph+) |
| `twitter:card` | Tells Twitter to show a large image card |

## Dynamic tags from views

Override the sections in child views to set page-specific values from your model data:

### Blog posts

```html
<!-- resources/views/posts/show.blade.php -->
@extends('layouts.app')

@section('og_title', $post->title)
@section('og_description', $post->excerpt)
@section('og_type', 'article')

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

### Products

```html
<!-- resources/views/products/show.blade.php -->
@extends('layouts.app')

@section('og_title', $product->name)
@section('og_description', '$' . number_format($product->price, 2) . ' - ' . $product->description)
@section('og_type', 'product')

@section('content')
  <h1>{{ $product->name }}</h1>
@endsection
```

## Verify

Start your development server and view the page source. Check that all `og:` meta tags are present with the correct values.

Open the [preview tool](/previews/new) and paste a URL from your site to see the social card image.

## Multiple domains

Running staging and production, subdomains, or several sites under one account? See [Multiple domains & environments](/docs/multiple-domains). For a single site, the connection tag above is all you need.


## Customize

OpenGraph+ captures your page in a headless browser and renders it as an image. You control what gets captured using meta tags in your layout and CSS in your stylesheets.

All of the rendering options below are standard HTML meta tags and CSS, so they work the same regardless of framework. The [HTML, CSS, & HTTP guide](/docs/html-css) covers each one in detail. This page shows how to wire them into a Laravel app.

## Meta tags

Add these to your layout's `<head>` alongside your `og:image` tag. They're all optional.

```html
<!-- resources/views/layouts/app.blade.php -->
<head>
  <meta property="og:image" content="https://$OGPLUS_KEY.ogplus.net/{{ request()->path() }}">
  <meta name="twitter:card" content="summary_large_image">

  {{-- Render at 800px wide instead of the default --}}
  <meta property="og:plus:viewport:width" content="800">

  {{-- Only capture this element instead of the full page --}}
  <meta property="og:plus:selector" content=".post-header">

  {{-- Inject inline styles on the captured element --}}
  <meta property="og:plus:style" content="padding: 60px; background: #0f172a; color: white;">
</head>
```

See the [Rendering](/docs/html-css/rendering) guide for what each meta tag does and how they interact.

## CSS styling

OpenGraph+ adds a `data-ogplus` attribute to your `<html>` element during capture. Use it in your stylesheets to hide navigation, adjust spacing, or restyle anything for the social card without affecting your actual site.

```css
/* resources/css/app.css */
html[data-ogplus] {
  nav { display: none; }
  footer { display: none; }
  .hero { padding: 60px; }
}
```

If you're using Tailwind, there's a plugin that gives you `ogplus:` variants like `ogplus:hidden` and `ogplus-twitter:bg-sky-500`. See [CSS Styling](/docs/html-css/data-attributes) for plain CSS examples and [Tailwind setup](/docs/html-css/data-attributes#tailwind-css).

## Templates

For fully custom social card layouts that pull content from your page, use `<template>` elements. These let you build a completely different layout for screenshots without touching your visible page.

```html
{{-- resources/views/layouts/app.blade.php --}}
<template id="ogplus">
  <div style="padding: 48px; background: #0f172a; color: white; height: 100%;">
    <h1 style="font-size: 48px;">
      ${document.querySelector('h1')?.textContent}
    </h1>
  </div>
</template>
```

See the [Templates](/docs/html-css/templates) guide for expression syntax, platform-specific templates, and full examples.

## Testing locally

The [Preview Bookmarklet](/docs/html-css/bookmarklet) sets the `data-ogplus` attribute in your browser so you can see how your CSS and Tailwind variants look without deploying or waiting for a real crawler to hit your page.

## Full example

A blog post layout with all the pieces together:

```html
<head>
  <meta property="og:title" content="@yield('og_title', 'My Site')">
  <meta property="og:description" content="@yield('og_description', 'Welcome')">
  <meta property="og:image" content="https://$OGPLUS_KEY.ogplus.net/{{ request()->path() }}">
  <meta name="twitter:card" content="summary_large_image">
  <meta property="og:plus:selector" content=".post-header">
  <meta property="og:plus:style" content="padding: 60px; background-color: #0f172a; color: white;">
  <meta property="og:plus:viewport:width" content="800">
</head>
```


## Caching

OpenGraph+ reads HTTP cache headers from your Laravel responses to decide when to re-render social card images. Laravel gives you several ways to control these headers.

This page covers the Laravel side. For how OpenGraph+ handles caching at the HTTP level, see the [HTTP Caching](/docs/html-css/caching) guide.

## Response headers

Set cache headers directly in your controller methods:

```php
public function show(Post $post)
{
    return response()
        ->view('posts.show', ['post' => $post])
        ->header('Cache-Control', 'public, max-age=86400');
}
```

This tells OpenGraph+ the content is valid for 24 hours before re-rendering.

## Middleware

Create a reusable middleware for cache headers across routes:

```php
// app/Http/Middleware/CacheControl.php
public function handle($request, Closure $next, $maxAge = 3600)
{
    $response = $next($request);
    $response->headers->set('Cache-Control', "public, max-age={$maxAge}");
    return $response;
}
```

Apply it to routes:

```php
Route::get('/posts/{post}', [PostController::class, 'show'])
    ->middleware('cache.control:86400');
```

## ETags

Laravel doesn't have built-in ETag helpers like some frameworks, but you can set them manually:

```php
public function show(Post $post)
{
    return response()
        ->view('posts.show', ['post' => $post])
        ->header('Cache-Control', 'public, max-age=3600')
        ->header('ETag', '"' . md5($post->updated_at) . '"');
}
```

If your content hasn't changed, OpenGraph+ serves the cached image without re-rendering.

## Recommendations

| Page type | Strategy |
|-----------|----------|
| Static pages | `Cache-Control: public, max-age=604800` (1 week) |
| Blog posts | `Cache-Control: public, max-age=86400` (1 day) |
| Index pages | `Cache-Control: public, max-age=3600` (1 hour) |
| User dashboards | No caching or `Cache-Control: no-store` |

## Meta tag overrides

If you can't control HTTP headers, use meta tags instead. See the [HTTP Caching guide](/docs/html-css/caching) for the full list of meta tag overrides.

## Purging cached images

When you need to force a refresh immediately, go to your website dashboard, find the page, and click purge. This clears the cached image and triggers a re-render on the next request.


## Troubleshooting

Something not working? Here are the most common issues and how to fix them.

## Meta tags not appearing

Check that your view extends the layout that has the meta tags. View your page source to verify the `og:image` and `twitter:card` tags are present in the `<head>`.

Common causes:

1. Your view doesn't extend `layouts.app` (or whichever layout has the meta tags)
2. The `@yield('content')` or `{{ $slot }}` section is missing from the layout

## Leading slash in path

`request()->path()` returns the path without a leading slash (e.g., `posts/1` not `/posts/1`). Make sure your meta tag includes the `/` between the connection URL and the path:

```html
{{-- Correct --}}
<meta property="og:image" content="https://$OGPLUS_KEY.ogplus.net/{{ request()->path() }}">

{{-- Wrong - missing slash --}}
<meta property="og:image" content="https://$OGPLUS_KEY.ogplus.net{{ request()->path() }}">
```

## Wrong image showing

This is almost always a caching issue. Social platforms and OpenGraph+ both cache images.

1. Open the [preview tool](/previews/new)
2. Paste your URL to see what OpenGraph+ currently has
3. If the image is stale, purge it from the dashboard
4. Re-check with the preview tool

## Social platforms not updating

Twitter, LinkedIn, and Slack cache images aggressively on their end. After confirming the correct image appears in the [preview tool](/previews/new):

- **Twitter:** Use the [Card Validator](https://cards-dev.twitter.com/validator) to force a refresh
- **LinkedIn:** Use the [Post Inspector](https://www.linkedin.com/post-inspector/) to clear their cache
- **Facebook:** Use the [Sharing Debugger](https://developers.facebook.com/tools/debug/) to scrape again

This is platform-side caching that OpenGraph+ cannot control.

## Purging cached images

1. Go to your website dashboard in OpenGraph+
2. Find the page you want to refresh
3. Click purge to clear the cached image

The next request from a social platform will trigger a fresh render.

## Testing

Use the [preview tool](/previews/new) to verify your setup before sharing URLs. This shows you exactly what social platforms will see.

