Blog Detail

27

Apr
Easily Handle SEO in Any Big or Small Laravel Application cover image

arrow_back Easily Handle SEO in Any Big or Small Laravel Application

Currently, there aren’t that many SEO packages for Laravel and the available ones are quite complex to set up and very decoupled from the database. They only provided you with helpers to generate the tags, but you still had to use those helpers: nothing was generated automatically and they almost do not work out of the box.

Laravel Seo package generates valid and useful meta tags straight out of the box, with limited initial configuration, whilst still providing a simple, but powerful API to work with. It can generate:

  1. Robots tag
  2. Title tag (with sitewide suffix)
  3. Meta tags (author, description, image, etc.)
  4. OpenGraph Tags (Facebook, LinkedIn, etc.)
  5. Twitter Tags
  6. Structured data (Article and Breadcrumbs)
  7. Favicon

Installation

Run the following command to install the package:

composer require ralphjsmit/laravel-seo

Publish the migration and configuration file:

php artisan vendor:publish --tag="seo-migrations"
php artisan vendor:publish --tag="seo-config"

Next, go to the newly published config file in config/seo.php and make sure that all the settings are correct.

Usage

After installation, Now you have to add the following Blade code on every page where you want your SEO tags to appear:

{!! seo() !!}

This will render a lot of sensible tags by default, already greatly improving your SEO. It will also render things like the title tag, so you don’t have to render that manually.

To really profit from this package, you can associate an Eloquent model with an SEO model. This will allow you to dynamically fetch SEO data from your model and this package will generate as many tags as possible for you, based on that data.

To associate an Eloquent model with an SEO model, add the HasSEO trait to your model:

use RalphJSmit\Laravel\SEO\Support\HasSEO;

class Post extends Model
{
    use HasSEO;
    
    // ...

This will automatically create and associate a SEO model for you when a Post is created. You can also manually create a SEO-model for a Post, use the ->addSEO() method for that ($post->addSEO()).

You’ll be able to retrieve the SEO model via the Eloquent SEO relationship:

$post = Post::find(1);

$seo = $post->SEO;

On the SEO model, you may update the following properties:

  • title
  • description
  • author
  • image
$post = Post::find(1);

$post->seo->update([
   'title' => 'My title for the SEO tag',
   'image' => 'images/posts/1.jpg', // Will point to `public_path('images/posts/1.jpg')`
]);

However, it can be a bit cumbersome to manually update the SEO model every time you make a change. That’s why I provided the getDynamicSEOData() method, which you can use to dynamically fetch the correct data from your own model and pass it to the SEO model:

public function getDynamicSEOData(): SEOData
{
    return new SEOData(
        title: $this->title,
        description: $this->excerpt,
        author: $this->author->fullName,
    );
}

You are allowed to only override the properties you want and omit the other properties (or pass null to them). You can use the following properties:

  1. title This will be used for the title tag and all the related tags (OpenGraph, Twitter, etc.)
  2. description this will be used for the meta description tag and all the related tags (OpenGraph, Twitter, etc.)
  3. author (should be the author’s name)
  4. image (should be the image path and be compatible with url=publicpath(url = public_path(path))
  5. url (by default it will be url()->current())
  6. enableTitleSuffix (should be true or false, this allows you to set a suffix in the config/seo.php file, which will be appended to every title)
  7. site_name
  8. published_time (should be a Carbon instance with the published time. By default this will be the created_at property of your model)
  9. modified_time (should be a Carbon instance with the published time. By default this will be the updated_at property of your model)
  10. section (should be the name of the section of your content. It is used for OpenGraph article tags and it could be something like the category of the post)
  11. tags (should be an array with tags. It is used for the OpenGraph article tags)
  12. schema (this should be a SchemaCollection instance, where you can configure the JSON-LD structured data schema tags)
  13. locale (this should be the locale of the page. By default this is derived from app()->getLocale() and it looks like en or nl.)

Finally, you should update your Blade file, so that it can receive your model when generating the tags:

{!! seo()->for($page) !!}
{{-- Or pass it directly to the `seo()` method: --}}
{!! seo($page ?? null) !!}

With this package, you can also generate JSON-LD structured data. This package has a lot of advanced features with code examples. If you want to dig more, you can visit its complete documentation on Github.

Published at : 27-04-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project