A package to manage meta fields on Laravel Eloquent models.
- PHP >= 8.2
- Laravel 10.0
You can install the package via composer:
composer require novius/laravel-meta
Optionally you can also:
php artisan vendor:publish --provider="Novius\LaravelMeta\LaravelMetaServiceProvider" --tag=lang
php artisan vendor:publish --provider="Novius\LaravelMeta\LaravelMetaServiceProvider" --tag=views
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('text');
$table->timestamps();
$table->addMeta(); // Macro provided by the package
});
namespace App\Models;
use \Illuminate\Database\Eloquent\Model;
use Novius\LaravelMeta\Traits\HasMeta;
class Post extends Model {
use HasMeta;
...
}
You can also add this method which will define the default operation of the trait
public function getMetaConfig(): MetaModelConfig
{
if (! isset($this->metaConfig)) {
$this->metaConfig = MetaModelConfig::make()
->setDefaultSeoRobots(IndexFollow::index_follow) // The default value of the seo_robots field if not defined
->setFallbackTitle('title') // The name of field for the default value of the seo_title and og_title fields if not defined. Can also be a callable, see below
->setFallbackDescription(function($model) { // The default value of the seo_description and og_description fields if not defined. Can also be a string, see above
return $model->description;
})
->setFallbackImage('picture')
->setCallbackOgImageUrl(function($model) { // The function to get the og_image url
if ($model->og_image) {
return asset('storage/'.$model->og_image);
}
return null;
})
->setOgImageDisk('a_disk')
->setOgImagePath('/og-image/');
}
return $this->metaConfig;
}
$model = ModelHasMeta::first();
$model->canBeIndexedByRobots();
$model->seo_robots
$model->seo_title
$model->seo_description
$model->seo_keywords
$model->og_type
$model->og_title
$model->og_description
$model->og_image
$model->og_image_url
$indexableByRobots = Post::query()->indexableByRobots();
$notIndexableByRobots = Post::query()->notIndexableByRobots();
If you use Laravel Nova, you can do that on your Resource on a Model using HasMeta :
<?php
use Novius\LaravelMeta\Traits\NovaResourceHasMeta;
class HasMetaModel extends Resource
{
use NovaResourceHasMeta;
/**
* Get the fields displayed by the resource.
*
* @return array
*/
public function fields(NovaRequest $request)
{
return [
ID::make()->sortable(),
new Panel('Model', [
Text::make('Title', 'title'),
Textarea::make('Description', 'description'),
]),
new Panel('Meta', $this->getSEONovaFields([
'seo_keywords' => null, // This will not display field for seo_keywords
'og_image' => AlternativeImageField::make(trans('laravel-meta::messages.og_image'), $columnMeta.'->og_image')
->hideFromIndex(),
])->toArray()),
];
}
}
You can use de Facade CurrentModel to display the meta of a model.
In your controller :
use Novius\LaravelMeta\Facades\CurrentModel;
class HasModelController extends Controller
{
public function show($id)
{
$model = HasMetaModel::find($id);
CurrentModel::setModel($model);
return view('has-meta', compact('model'));
}
}
In the view :
@section('metas')
@include('laravel-meta::meta')
@endsection
<x-main-layout>
<div class="container mx-auto px-4 md:px-0">
<h1>{{ $model->title }}</h1>
<p>{{ $model->description }}</p>
</div>
</x-main-layout>
composer run test
Lint your code with Laravel Pint using:
composer run cs-fix
This package is under GNU Affero General Public License v3 or (at your option) any later version.