Handle SEO from your Laravel Models
Last updated on by Paul Redmond
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:
- Robots tag
- Title tag (with sitewide suffix)
- Meta tags (author, description, image, etc.)
- OpenGraph Tags (Facebook, LinkedIn, etc.)
- Twitter Tags
- Structured data (Article and Breadcrumbs)
- Favicon
This package connects SEO data to models. For example, if you have a Post
model, you can add/update/read SEO data:
$post = Post::find(1); $post->seo->update([ 'title' => 'My great post', 'description' => 'This great post will enhance your live.',]); // Access the SEO relationship$seo = $post->seo;
You can also dynamically retrieve this data in a model using the following method:
use RalphJSmit\Laravel\SEO\Support\HasSEO; class Post extends Model{ use HasSEO; public function getDynamicSEOData(): SEOData { $pathToFeaturedImageRelativeToPublicPath = // ..; // Override only the properties you want: return new SEOData( title: $this->title, description: $this->excerpt, image: $pathToFeaturedImageRelativeToPublicPath, ); }}
Then on the frontend you can use the seo()
helper to render HTML tags:
<!DOCTYPE html><html><head> {!! seo()->for($post) !!} {{-- No need to separately render a <title> tag or any other meta tags! --}}</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.