There is no license information available for the latest version (v1.1.1) of this package.

Store meta data for the web against your Laravel Models

v1.1.1 2019-08-14 14:40 UTC

This package is auto-updated.

Last update: 2024-04-15 02:52:18 UTC


README

Provides a standardised approach to attaching website meta data to Laravel Models. Typically this is used for entities such as Pages, Blog Posts, Events, etc, which will need fields like 'Meta Title', 'Meta Description', and 'OG Image' when they are presented on the web.

Installation

You can install the package via composer:

composer require optimuscms/meta

Once installed, you should add the tables used by this package to your database:

php artisan migrate

Key concepts

There are a few key concepts that should be understood before continuing:

  • A new model called Meta will be attached to whichever Models you define as requiring meta data.

  • It is a 'has one' relationship where the Model can have zero or one Meta Model attached to it.

  • The Meta Model depends on the optimuscms/media package in order to attach images to it.

Usage

  1. Add the HasMeta trait to whatever Model you want to collect meta data for:

    class MyModel extends Model
    {
        use Optix\Meta\HasMeta;
  2. Add a boot method to your Model (if it doesn't already exist) with the following content:

    protected static function boot()
    {
        parent::boot();
    
        static::saved(function ($model) {
            /** @var HasMeta $model */
            $model->saveMeta(request('meta'));
        });
    }
  3. (Optional) If your Model appears in an API response, you'll want to add the meta property (either to the Model directly, or in a Resource class if you're using one):

    public function toArray()
    {
       return [
           ...other properties,
           'meta' => $this->meta,
       ];
    }
  4. When creating or updating your model (eg. from a CMS action), make sure your form submits its request in the following format:

    {
        ...MODEL_FIELDS,
        meta: {
            title, // max 100 chars
            description, // max 200 chars
            og_title, // max 100 chars
            og_description, // max 200 chars
            og_image_id, // the id of a Media Model
            custom_tags // free text field for adding custom HTML
        }
    }

    All fields are optional and this package will automatically pick them up and process them as required.

Retrieving OG images

This package also provides a convenient way to retrieve the OG image as a Media Model:

$media = $myModel->meta->getOgImage();

License

The MIT License (MIT). Please see License File for more information.