Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Meta Laravel Package

novius/laravel-meta

View on GitHub
Deep Wiki
Context7

Laravel Meta

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

Filament

If you use Filament, you can do that on your Resource on a Model using HasMeta :

<?php

use Novius\LaravelMeta\Traits\FilamentResourceHasMeta;


class HasMetaModel extends Resource
{
    use FilamentResourceHasMeta;

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                ...static::getFormSEOFields(),
            ]);
    }
}

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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity