Author Login

Advanced Laravel SEO Optimization Toolkit

๐Ÿ‘ค Aisha Mbaye
20 Views
Oct 14, 2025
Contents

Why SEO Matters

SEO (Search Engine Optimization) improves your siteโ€™s visibility on Google. In Laravel, SEO should be baked into every dynamic page (snippets, blog posts, etc.) using metadata and structured data.

Core SEO Elements

The most important elements include:

  • <title>: Unique per page
  • <meta name="description">
  • <meta name="keywords">
  • <link rel="canonical">
  • Open Graph tags for social sharing


Create a Blade SEO Component

This component dynamically injects meta tags based on variables passed from the controller.

Code Example (resources/views/components/seo.blade.php):

Language: BLADE
<title>{{ $title ?? config('app.name') }}</title>
<meta name="description" content="{{ $description ?? 'Default meta description for your site.' }}">
<meta name="keywords" content="{{ $keywords ?? 'laravel, seo, optimization' }}">
<link rel="canonical" href="{{ $canonical ?? url()->current() }}">


{{-- Open Graph --}}
<meta property="og:title" content="{{ $title ?? config('app.name') }}">
<meta property="og:description" content="{{ $description ?? 'Default meta description for your site.' }}">
<meta property="og:url" content="{{ $canonical ?? url()->current() }}">
<meta property="og:type" content="website">
<meta property="og:image" content="{{ $image ?? asset('images/og-default.png') }}">

Pass Meta Data from Controller

You can set dynamic meta info inside any controller and pass it to the view.

Code Example (SnippetController.php):

Language: PHP
public function show(Snippet $snippet)
{
  $seo = [
    'title' => $snippet->title . ' | SuriSnippet',
    'description' => Str::limit(strip_tags($snippet->about), 155),
    'keywords' => implode(',', $snippet->tags->pluck('name')->toArray()),
    'canonical' => route('frontend.snippet.show', $snippet->slug),
    'image' => asset('storage/' . $snippet->thumbnail),
  ];


  return view('frontend.snippets.show', compact('snippet', 'seo'));
}

Include SEO Component in Blade

Now, include the SEO component inside your page layout head.

Code Example (resources/views/frontend/snippets/show.blade.php):

Language: BLADE
@extends('layouts.frontend')


@section('title', $seo['title'])
@section('meta')
 <x-seo 
  :title="$seo['title']"
  :description="$seo['description']"
  :keywords="$seo['keywords']"
  :canonical="$seo['canonical']"
  :image="$seo['image']"
 />
@endsection

Create a Sitemap Route

Google and Bing read your sitemap to discover URLs. We'll generate it dynamically.

Code Example (routes/web.php):

Language: PHP
use App\Models\Snippet;


Route::get('/sitemap.xml', function () {
  $snippets = Snippet::where('is_active', true)->get();


  return response()->view('sitemap', compact('snippets'))
    ->header('Content-Type', 'application/xml');
});

Sitemap Blade View

This XML file lists your pages dynamically.

Code Example (resources/views/sitemap.blade.php):

Language: BLADE
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
 @foreach($snippets as $snippet)
 <url>
  <loc>{{ route('frontend.snippet.show', $snippet->slug) }}</loc>
  <lastmod>{{ $snippet->updated_at->toAtomString() }}</lastmod>
  <changefreq>weekly</changefreq>
  <priority>0.8</priority>
 </url>
 @endforeach
</urlset>

Adding OG & Twitter Tags

These tags control how your link looks when shared on social media.

Language: BLADE
<meta property="og:title" content="{{ $seo['title'] }}">
<meta property="og:description" content="{{ $seo['description'] }}">
<meta property="og:image" content="{{ $seo['image'] }}">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="{{ $seo['title'] }}">
<meta name="twitter:description" content="{{ $seo['description'] }}">
<meta name="twitter:image" content="{{ $seo['image'] }}">

Performance Tips

Explanation:

  • Minimize duplicate meta tags.
  • Always use HTTPS in canonical URLs.
  • Include OG images of 1200x630px for social share.
  • Cache your sitemap for performance.


Bonus โ€“ Auto SEO Settings from Admin

If your platform (like SuriSnippet) has a settings table, you can auto-load global SEO defaults.

Code Example (AppServiceProvider.php):

Language: PHP
use App\Models\SiteSetting;


public function boot(): void
{
  $settings = cache()->remember('seo_settings', 3600, fn() => SiteSetting::pluck('value', 'key'));
  view()->share('default_seo', $settings);
}

Then, your <x-seo> component can fall back to $default_seo if no values are passed.

โœ… Summary

This snippet helps users:

  • Dynamically generate meta tags and OG previews.
  • Generate and serve an XML sitemap automatically.
  • Reuse a Blade <x-seo> component site-wide.
  • Improve SEO visibility and indexing for Laravel projects.


About SuriSnippet

Know about our company more.

Contact Us

We are Here to Help

FAQ

Get all Answers