If you are building a Laravel website and want to make it SEO-friendly, then you don’t need to write all the meta tags manually. There is a simple package called ralphjsmit/laravel-seo which helps you manage titles, descriptions, Open Graph, Twitter cards, canonical tags and more. In this blog, I will explain step by step how to install, configure, and use it in both static pages and dynamic posts.
First, install the package with Composer:
composer require ralphjsmit/laravel-seo
Then publish the migration and configuration files:
php artisan vendor:publish --tag="seo-migrations"
php artisan vendor:publish --tag="seo-config"
Now run the migration:
php artisan migrate
This will create a table where SEO data can be stored for your models.
After publishing, you will see a new file at config/seo.php
. Open it and check all the default values. Here you can define your site name, sitemap path, favicon, robots meta tag, default title suffix, and fallback values like description or image.
To make your model SEO-ready, use the HasSEO
trait. For example, in your Post
model:
use RalphJSmit\Laravel\SEO\Support\HasSEO;
use RalphJSmit\Laravel\SEO\Support\SEOData;
class Post extends Model
{
use HasSEO;
public function getDynamicSEOData(): SEOData
{
return new SEOData(
title: $this->seo->title ?? $this->title,
description: $this->seo->description,
image: $this->seo->image
);
}
// handle delete logic
protected static function booted(): void
{
// This 'deleting' event fires right before an item is deleted.
static::forceDeleted(function (Post $post) {
// Check if SEO data exists before trying to delete it.
if ($post->seo) {
$post->seo->delete();
}
});
}
}
This way, each post will automatically generate SEO data from its title, description, and image.
When you create or update a post, you can also save custom SEO values. For example:
use RalphJSmit\Laravel\SEO\Support\SEOData; //import this
$post = Post::create($data);
// add seo
$post->seo->update([
'title' => $request->seo_title,
'description' => $request->seo_description,
'image' => $post->getFirstMediaUrl('image'),
]);
This lets you control SEO fields separately from the main content
Now you need to display SEO tags in your main layout blade file (resources/views/layouts/app.blade.php
) inside the <head>
tag.
@section('seo')
{!! seo() !!}
@show
This will output <title>
, <meta>
, and OpenGraph tags.
If you have a page like “About Us” without a model, you can directly pass SEO data from the controller:
use RalphJSmit\Laravel\SEO\Support\SEOData;
public function about()
{
return view('frontend.about', [
'SEOData' => new SEOData(
title: 'About Us',
description: 'Learn more about our company',
image: ''
)
]);
}
And in your about.blade.php
view:
@section('seo')
{!! seo($SEOData) !!}
@endsection
For blog posts or products, you can simply call:
@section('seo')
{!! seo()->for($post) !!}
@endsection
This will fetch SEO data from the model automatically.
The ralphjsmit/laravel-seo package makes SEO very easy in Laravel projects. With just a few lines, you can add proper titles, meta descriptions, Open Graph tags, and make your site look professional on Google, Facebook, and Twitter.
👉 Official package link: https://github.com/ralphjsmit/laravel-seo
If you want a custom Laravel website with SEO setup from the start, you can contact us for development.
📱 WhatsApp: https://wa.me/918447525204