PHPackages                             novius/laravel-meta - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. novius/laravel-meta

ActiveLibrary[Database &amp; ORM](/categories/database)

novius/laravel-meta
===================

Help to manage meta data on Laravel Eloquent model

1.2.0(2mo ago)04.9k↓40.9%4AGPL-3.0-or-laterPHPPHP &gt;=8.2CI passing

Since Aug 2Pushed 2mo ago4 watchersCompare

[ Source](https://github.com/novius/laravel-meta)[ Packagist](https://packagist.org/packages/novius/laravel-meta)[ Docs](https://github.com/novius/laravel-meta)[ RSS](/packages/novius-laravel-meta/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (7)Versions (6)Used By (4)

Laravel Meta
============

[](#laravel-meta)

[![Novius CI](https://github.com/novius/laravel-meta/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/novius/laravel-meta/actions/workflows/main.yml)[![Packagist Release](https://camo.githubusercontent.com/a5c0b950182380844543ca16988088ab1b13e866d1a2d136b4e7d85d5860968a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e6f766975732f6c61726176656c2d6d6574612e7376673f6d61784167653d31383030267374796c653d666c61742d737175617265)](https://packagist.org/packages/novius/laravel-meta)[![License: AGPL v3](https://camo.githubusercontent.com/c61341f63648cdd5aba4f7a073b513106a63778c27b15f96c56157642bc943b4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4147504c25323076332d626c75652e737667)](http://www.gnu.org/licenses/agpl-3.0)

Introduction
------------

[](#introduction)

A package to manage meta fields on Laravel Eloquent models.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.2
- Laravel 10.0

Installation
------------

[](#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
-----

[](#usage)

#### Migrations

[](#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

[](#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

[](#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

[](#nova)

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

```
