fomvasss / laravel-seo
Laravel SEO package
1.7.0
2024-03-20 16:03 UTC
Requires
- php: ^8.0
- illuminate/contracts: ^9|^10|^11
README
With this package you can manage meta-tags and SEO-fields from Laravel app.
Installation
You can install the package via composer:
composer require fomvasss/laravel-seo
You can publish and run the migrations with:
php artisan vendor:publish --provider="Fomvasss\Seo\SeoServiceProvider"
php artisan migrate
Usage
The Eloquent model must has the Trait HasSeo
:
namespace App\Models; use Fomvasss\Seo\Models\HasSeo; class PostModel extends Model { use HasSeo; //... public function registerSeoDefaultTags(): array { return [ 'title' => $this->name, 'description' => $this->description, 'og_image' => $this->getFirstMediaUrl('images', 'thumb'), ]; } }
Also in model you can define default tags in method registerSeoDefaultTags
Allowed next methods (By increasing priority):
Seo::setDefault(['title' => 'Blog']); Seo::setModel($post); Seo::setPath('page/faq'); Seo::setTags(['keywords' => 'Laravel, SEO, tags']);
Rendering tags in Blade (in HTML head):
{!! \Seo::setGroup(app()->getLocale()) ->setDefault([ 'og_site_name' => config('app.name'), 'og_url' => URL::full(), 'og_locale' => app()->getLocale(), ])->renderHtml() !!}
Get array tags (for API resource, etc.):
\Seo::setGroup(app()->getLocale()) ->setDefault([ 'og_site_name' => config('app.name'), 'og_locale' => app()->getLocale(), ])->getTags();
You can save tags for model in DB:
$post->seo('uk')->updateOrCreate([], ['tags' => ['title' => 'Hello Page', 'description' => 'Lorem Ipsum'], 'group' => 'uk']);
And get tags (for edit) for dashboard:
$tags = $post->getRawSeoTags('uk');
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.