LaravelMetaTags
The most powerful and extendable tools for managing SEO Meta Tags in your Laravel project
Install / Use
/learn @butschster/LaravelMetaTagsREADME

The most powerful and extendable tools for managing SEO Meta Tags in your Laravel project
Laravel SEO Meta Tags offers a sophisticated solution for Laravel applications, allowing developers to seamlessly manage header meta tags, CSS, JavaScript, and other relevant tags. Its primary objective is to simplify the process of managing search engine optimization (SEO) tags within your application.
Moreover, its versatile API makes it compatible with frameworks like Inertiajs, VueJS and other JavaScript frameworks.
For any questions or further assistance, please join our official telegram group
Contributing
We enthusiastically invite you to contribute to the package! Whether you've uncovered a bug, have innovative feature suggestions, or wish to contribute in any other capacity, we warmly welcome your participation. Simply open an issue or submit a pull request on our GitHub repository to get started.
Remember, every great developer was once a beginner. Contributing to open source projects is a step in your journey to becoming a better developer. So, don't hesitate to jump in and start contributing!
We appreciate any contributions to help make the package better!
Supercharge Your Development with Buggregator
Pair it with our tool for a more robust development environment. For more information visit buggregator.dev <img src="https://github.com/buggregator/.github/assets/773481/24981ab5-510a-453c-a3c5-8a6f5e7bf358">
Features
- Meta Management: Effortlessly set titles, charset, pagination links, and more.
- Styles & Scripts: Organize and place styles and scripts anywhere in your HTML.
- Custom Tags: Make your own tags to suit specific needs.
- Rich Media Integration: Supports both Open Graph & Twitter Cards.
- Analytics Ready: Comes with Google Analytics and Yandex Metrika tracking code support, including a code builder for the latter.
- Site Verification: Supports webmaster tools site verifier tags.
- Package System: Group tags, styles, and scripts into named packages for easy inclusion anywhere.
- Robust Documentation: Clear instructions and guidelines for a seamless setup.
- Thoroughly Tested: Built to ensure reliability and stability.
Requirements
- Laravel version: 9.x to 11.x
- PHP version: 8.0 or higher
Installation and Configuration
- Install the package: Use the following command to install the Meta Tags package in your awesome application.
composer require butschster/meta-tags
- Register the Service Provider: After installing the package, you must register its service provider.
You can do it using the following artisan command:
php artisan meta-tags:install
This command will activate the App\Providers\MetaTagsServiceProvider and publish the configuration
at config/meta_tags.php. Within this configuration file, you can set default titles, keywords, descriptions, and other
meta tags which will be automatically inserted into your HTML.
- Verification: Ensure that
App\Providers\MetaTagsServiceProviderhas been added to the providers array in yourconfig/app.phpconfiguration file. If it hasn't, you'll need to add it manually. Remember, if your application isn't using theAppnamespace, update the provider class name accordingly.
And that's all! Your Laravel project is now equipped to handle SEO meta tags with ease.
Usage
Controller
You can use either Facade \Butschster\Head\Facades\Meta or \Butschster\Head\Contracts\MetaTags\MetaInterface in your
controller
use Butschster\Head\MetaTags\MetaInterface;
class HomeController extends Controller
{
public function __contruct(
protected MetaInterface $meta
) {
}
public function index()
{
$news = News::paginate();
// Prepend title part to the default title
$this->meta
// Will render "Home page - Default Title"
->prependTitle('Home page')
// Will include next, prev, canonical links
->setPaginationLinks($news)
// Will change default favicon
->setFavicon('/favicon-index.ico')
}
}
// Or you can use the facade
use Butschster\Head\Facades\Meta;
class HomeController extends Controller
{
public function index()
{
$news = News::paginate();
Meta::prependTitle('Home page')
->setPaginationLinks($news)
->setFavicon('favicon-index.ico');
}
}
View
To integrate meta tags into your HTML, simply insert {!! Meta::toHtml() !!} wherever required.
Note You have two options to insert meta tags:
{!! Meta::toHtml() !!}or the Blade directive@meta_tags.
Here's an example of how you can use it in your view:
<!DOCTYPE html>
<html lang="en">
<head>
{!! Meta::toHtml() !!}
</head>
...
</html>
Placements
The package provides flexibility to insert meta tags beyond the header. You can define specific placements in your templates.
- To place meta tags in the footer, use:
Meta::footer()->toHtml() - For custom placements, use:
Meta::placement('placement_name')->toHtml() - Alternatively, the Blade directive can also be used:
@meta_tags('placement_name')
<body>
...
{!! Meta::placement('middle_of_the_page')->toHtml() !!}
...
{!! Meta::footer()->toHtml() !!}
</body>
Packages
To avoid code repetition and improve code organization, you can group tags, assets, etc., into named packages. This allows for streamlined inclusion of sets of meta tags or assets where needed.
To create a new package:
- Navigate to the Service provider.
- Add your package within
\App\Providers\MetaTagsServiceProvider.
To define packages, you can use the PackageManager::create method:
namespace App\Providers;
use Butschster\Head\Facades\PackageManager;
use Butschster\Head\Packages\Package;
use Illuminate\Support\ServiceProvider;
class MetaTagsServiceProvider extends ServiceProvider
{
...
protected function packages()
{
PackageManager::create('jquery', function(Package $package) {
$package->addScript(
'jquery.js',
'https://code.jquery.com/jquery-3.3.1.min.js',
['defer']
);
});
PackageManager::create('calendar', function(Package $package) {
$package->requires('jquery');
$package->addScript(
'fullcalendar.js',
'https://cdn.jsdelivr.net/npm/@fullcalendar/core@4.0.1/main.min.js',
['defer']
)->addScript(
'fullcalendar.locales.js',
'https://cdn.jsdelivr.net/npm/@fullcalendar/core@4.0.1/locales-all.min.js',
['defer']
)->addStyle(
'fullcalendar.css',
'https://cdn.jsdelivr.net/npm/@fullcalendar/core@4.0.1/main.min.css'
);
});
}
...
}
Using Packages in Controllers
When specific packages are required within a controller, you can include them by referencing the package name:
use Butschster\Head\Facades\Meta;
class EventsController extends Controller {
public function show(Event $event)
{
// Will include all tags from calendar package
Meta::includePackages(['calendar', ...]);
}
}
Setting Global Packages
For packages that should be included on every page, you can define them in the config/meta_tags.php:
...
'packages' => [
'jquery', 'calendar', ...
],
...
And there you have it! With these steps, handling and organizing meta tags and assets becomes a breeze.
Note All methods available in the
Metaclass can also be utilized alongside these package functions.
API
Meta
\Butschster\Head\MetaTags\Meta
- This class implements
Illuminate\Contracts\Support\Htmlableinterface
Methods
Set the main part of meta title
Meta::setTitle('Laravel');
// <title>Laravel</title>
// You can specify max length. (By default it gets from config.)
Meta::setTitle('Laravel', 4);
// <title>Lara...</title>
Prepend title part to main title
Meta::setTitle('Laravel')
->prependTitle('Home page');
// <title>Home page - Laravel</title>
Set the title separator
By default it gets from config
Meta::setTitleSeparator('->')
->setTitle('Laravel')
->prependTitle('Home page');
// <title>Home page -> Laravel</title>
Set the description
Meta::setDescription('Awesome page');
// <meta name="description" content="Awesome page">
// You can specify max length. (By default it gets from config.)
Meta::setDescription('Awesome page', 7);
// <meta name="description" content="Awesome...">
Set the keywords
Meta::setKeywords('Awesome keywords');
// <meta name="keywords" content="Awesome keywords">
Meta::setKeywords(['Awesome keyword', 'keyword2']);
// <meta name="keywords" content="Awesome keyword,
