Handle SEO for Your Models With this Laravel Package

0
31
laravel-seo-featured.png


The laravel-seo package handles the SEO in any Laravel application, big or small:

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.

With a bit of configuration, this package offers the following out of the box:

  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

This package connects SEO data to models. For example, if you have a Post model, you can add/update/read SEO data:

1$post = Post::find(1);

2 

3$post->seo->update([

4 'title' => 'My great post',

5 'description' => 'This great post will enhance your live.',

6]);

7 

8// Access the SEO relationship

9$seo = $post->seo;

You can also dynamically retrieve this data in a model using the following method:

1use RalphJSmit\Laravel\SEO\Support\HasSEO;

2 

3class Post extends Model

4{

5 use HasSEO;

6 

7 public function getDynamicSEOData(): SEOData

8 {

9 $pathToFeaturedImageRelativeToPublicPath = // ..;

10 

11 // Override only the properties you want:

12 return new SEOData(

13 title: $this->title,

14 description: $this->excerpt,

15 image: $pathToFeaturedImageRelativeToPublicPath,

16 );

17 }

18}

Then on the frontend you can use the seo() helper to render HTML tags:

1<!DOCTYPE html>

2<html>

3<head>

4 {!! seo()->for($post) !!}

5 

6 {{-- No need to separately render a <title> tag or any other meta tags! --}}

7</head>

The package’s readme has more examples, including advanced usage details. You can learn more about this package, get full installation instructions, and view the source code on GitHub.



Source link

Leave a reply

Please enter your comment!
Please enter your name here