PHPackages                             drhtoo/laravel-metafield - 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. drhtoo/laravel-metafield

ActiveLibrary

drhtoo/laravel-metafield
========================

Laravel MetaField

v1.0.0(3y ago)028MITPHPPHP ^7.3|^8.0

Since May 10Pushed 2y ago1 watchersCompare

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

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Laravel Metafield Package
=========================

[](#laravel-metafield-package)

The Laravel Metafield package allows you to add custom metafields to your Laravel Eloquent models. This is useful when you need to store additional data about a model beyond its basic attributes.

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

[](#installation)

To install the package, you can simply run the following Composer command:

```
composer require drhtoo/laravel-metafield

```

### Publishing Config and Migration (Optional)

[](#publishing-config-and-migration-optional)

After you've installed the package, you can optinally publish the package's config and migrations to your Laravel application. You can do this by running the following command:

```
php artisan vendor:publish --provider="Drhtoo\MetaField\LaravelMetaFieldServiceProvider"

```

Once the migrations have been published, you can run them by using the following command:

```
php artisan migrate

```

Usage
-----

[](#usage)

To start using the Laravel Metafield package, you'll need to add the HasMeta trait to any Eloquent models that you want to have metafields. Here's an example:

```
// Product.php
use Drhtoo\MetaField\Models\Concerns\HasMeta;

class Product extends Model
{
    use HasMeta;
}

```

Now, you can use related meta fields by using relationship **metas**

```
$product = Product::find(1);

$product->metas->price = 100;

echo $product->metas->price; // 100

```

### MetaFields Property

[](#metafields-property)

To use meta fields as a model attribute, you have to add ***$metaFields*** property to your model. It is a protected property of array type which is the default values keyed by the field/key.

```
// Product.php
use Drhtoo\MetaField\Models\Concerns\HasMeta;

class Product extends Model
{
    use HasMeta;

    protected $metaFields = [
        'price' => null,
        'is_sale' => false,
        'sale_price' => null,
        'color' => 'white',
    ];
}

$product = Product::find(1);

$product->price = 100;

echo $product->price; // 100

```

### Attribute Casting

[](#attribute-casting)

You can also cast the metafield as in attributes of Eloquent/Models.

```
// Product.php
use Drhtoo\MetaField\Models\Concerns\HasMeta;

class Product extends Model
{
    use HasMeta;

    protected $metaFields = [
        'price' => null,
        'is_sale' => false,
        'sale_price' => null,
        'color' => null,
        'sale_start' => null,
        'sale_end' => null,
    ];

    protected $casts = [
        'price' => 'decimal:2',
        'sale_price' => 'decimal:2',
        'color' => 'array',
        'sale_start' => 'datetime',
        'sale_end' => 'datetime',
    ];
}

$product = Product::find(1);

$product->price = 100;

echo $product->price; // 100

```

### Working with Livewire Component

[](#working-with-livewire-component)

This package is well compatible with Livewire component and directly bind to wire:model attribute. To be able to livewire component find the metafield attributes, you need to append the fields to model attributes.

```
// Product.php
use Drhtoo\MetaField\Models\Concerns\HasMeta;

class Product extends Model
{
    use HasMeta;

    protected $metaFields = [
        'price' => null,
        'is_sale' => false,
        'sale_price' => null,
        'color' => null,
        'sale_start' => null,
        'sale_end' => null,
    ];

    protected $appends = [
        'price', 'is_sale', 'sale_price', 'color', 'sale_start', 'sale_end'
    ];
}

// Livewire component blade view

@error('product.price')
{{ $message }}
@enderror

```

### Working with Spatie's Laravel-translatable Package

[](#working-with-spaties-laravel-translatable-package)

Laravel Meta Field works well with spatie/laravel-translatable package and you just simply add array item of meta field to ***$translatable*** property of your model and use ***setAttribute*** method of ***HasMeta*** trait insteadof ***Translatable***.

```
// Product.php
use Drhtoo\MetaField\Models\Concerns\HasMeta;

class Product extends Model
{
    use HasMeta {
        HasMeta::setAttribute insteadof HasTranslations;
    }

    protected $attributes = [
        'title' => null,
        'description' => null,
    ];

    protected $metaFields = [
        'price' => null,
        'is_sale' => false,
        'sale_price' => null,
        'sale_description' => null,
    ];

    public $translatable = [
        'title',
        'description',
        'sale_description'
    ];
}

$product = Product::find(1);

$product->sale_description = 'This is sale product'; // set value to current locale

$product->setTranslation('sale_description', 'es', 'Este es un producto de venta');

```

That's all. Have fun.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1097d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e76818c4349ff4dd7f472d8d673a22e2d12eaa0ee1a0532d852a1180ccc04d98?d=identicon)[drhtoo](/maintainers/drhtoo)

---

Top Contributors

[![drhtoo](https://avatars.githubusercontent.com/u/73690910?v=4)](https://github.com/drhtoo "drhtoo (19 commits)")

### Embed Badge

![Health badge](/badges/drhtoo-laravel-metafield/health.svg)

```
[![Health](https://phpackages.com/badges/drhtoo-laravel-metafield/health.svg)](https://phpackages.com/packages/drhtoo-laravel-metafield)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
