novius/laravel-meta

Help to manage meta data on Laravel Eloquent model

1.0.2 2024-09-26 14:29 UTC

This package is auto-updated.

Last update: 2024-11-26 14:55:46 UTC


README

Novius CI Packagist Release License: AGPL v3

Introduction

A package to manage meta fields on Laravel Eloquent models.

Requirements

  • PHP >= 8.2
  • Laravel 10.0

Installation

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

Usage

Migrations

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('text');
    $table->timestamps();
    $table->addMeta(); // Macro provided by the package
});

Eloquent Model Trait

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;
    }

Extensions

$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();

Nova

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()),
        ];
    }
}

Front

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>

Testing

composer run test

CS Fixer

Lint your code with Laravel Pint using:

composer run cs-fix

Licence

This package is under GNU Affero General Public License v3 or (at your option) any later version.