A package to manage SEO (meta-tags, xml-fields, etc.)
fomvasss/laravel-meta-tags is a Laravel package for a package to manage seo (meta-tags, xml-fields, etc.).
It currently has 30 GitHub stars and 29.452 downloads on Packagist (latest version 3.5.2).
Install it with composer require fomvasss/laravel-meta-tags.
Discover more Laravel packages by fomvasss
or browse all Laravel packages to compare alternatives.
Last updated
Recommended: use fomvasss/laravel-seo.
If this package is useful to you, consider supporting its development:
USDT TRC20 address:
THLgp6DxiAtbNHvgnKV56vk1L38UuUagKf
This package lets you manage meta tags and SEO fields from Laravel controllers and Blade templates.
Run from the command line:
composer require fomvasss/laravel-meta-tags
php artisan vendor:publish --provider="Fomvasss\LaravelMetaTags\ServiceProvider"
config/meta-tags.php.database/migrations/DATE_NOW_create_meta_tags_table.php.resources/views/vendor/meta-tags/tags.blade.php.config/meta-tags.php (uncomment what you need).config/meta-tags.php.meta_tags migration and uncomment the fields you need.php artisan migrate
When upgrading from v2 to v3, please see the UPGRADING.md
app/Models/Article.phpAdd Metatagable trait in your entity model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Fomvasss\LaravelMetaTags\Traits\Metatagable;
class Article extends Model
{
use Metatagable;
//...
}
Controller example: app/Http/Controllers/ArticleController.php
MetaTag facade in controllers: app/Http/Controllers/ArticleController.php<?php
namespace App\Http\Controllers;
use MetaTag;
class ArticleController extends Controller
{
public function index()
{
$articles = \App\Models\Article::paginate();
MetaTag::setTags([
'title' => 'Article index page',
'description' => 'It is article index page',
]);
return view('index', compact('articles'));
}
public function store(Request $request)
{
// create entity
$article = \App\Models\Article::create($request->only([
//.. article data
]));
// create meta tag for entity
$article->metaTag()->create($request->only([
//.. meta tags fields
]));
}
public function show($id)
{
$article = \App\Model\Article::findOrFail($id);
// Set tags for showing
MetaTag::setEntity($article)
->setDefault([
'title' => $article->title, // if empty $article->metaTag->title - show this title
])->setTags([
'seo_text' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit',
'h1' => $article->title,
]);
return view('show', compact('article'));
}
public function search(Request $request)
{
$articles = \App\Models\Article::bySearch($request->q)
->paginate();
// Set tags for showing
MetaTag::setPath() // if argument `setPath()` is empty (or not set) - path = `request()->path()`
->setDefault([
'title' => 'Search page',
'robots' => 'noindex',
'og_title' => 'Search page OG',
'twitter_title' => 'Search page Twitter',
'canonical' => 'page/search',
]);
return view('index', compact('articles'));
}
}
For the package to work correctly, store only the URL path in the path field (without domain), with slashes trimmed (/).
Example:
https://site.com/some/pages/?page=23 => some/pageshttps://site.com/some/pages => /MetaTag facade in Blade templates: resources/views/layouts/app.blade.phpSimple and efficient:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
{!! MetaTag::render() !!}
</head>
<body>
@yield('content')
</body>
</html>
Or output one by one manually:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>{!! MetaTag::tag('title') !!}</title>
<meta name="description" content="{!! MetaTag::tag('description') !!}">
<meta name="keywords" content="{!! MetaTag::tag('keywords') !!}">
</head>
<body>
@yield('content')
</body>
</html>
Another example: resources/views/articles/show.blade.php
@extends('layouts.app')
@section('content')
<h1>{!! MetaTag::tag('title') !!}</h1>
<div>{!! $article->body !!}</div>
<div>{{ MetaTag::tag('seo_text') }}</div>
@endsection
And you can set meta tags right in the template:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
@php(MetaTag::setEntity($article))
@php(MetaTag::setDefault(['description' => 'My default meta tag']))
{!! MetaTag::render() !!}
</head>
<body>
@yield('content')
</body>
</html>
Similarly:
{!!
\MetaTag::setEntity($article)
->setDefault(['description' => 'My default meta tag'])
->render()
!!}
{!!
\MetaTag::setPath('articles')
->setDefault(['robots' => 'follow', 'canonical' => 'page/articles'])
->setDefault(['title' => 'All articles'])
->setDefault(['og_title' => 'All articles'])
->setDefault(['og_locale' => 'de'])
->setDefault(['og_image' => 'files/images/5be3d92e02a55890e4301ed4.jpg', 'og_image_height' => 123])
->render()
!!}