plank / laravel-metable
A package for attaching arbitrary data to Eloquent models
Installs: 523 276
Dependents: 5
Suggesters: 0
Security: 0
Stars: 281
Watchers: 10
Forks: 42
Open Issues: 8
Requires
- php: >=8.1
- ext-json: *
- illuminate/database: ^10.10|^11.0
- illuminate/support: ^10.10|^11.0
- phpoption/phpoption: ^1.8
Requires (Dev)
- guzzlehttp/guzzle: ^7.2
- guzzlehttp/promises: ^1.4
- laravel/legacy-factories: ^1.0.4
- mockery/mockery: ^1.4.2
- nesbot/carbon: ^2.62.1
- orchestra/testbench: ^8.0|^9.0
- php-coveralls/php-coveralls: ^2.7.0
- phpunit/phpunit: ^10.5
- symfony/symfony: ^6.1|^7.0
This package is auto-updated.
Last update: 2024-11-22 02:00:47 UTC
README
Laravel-Metable is a package for easily attaching arbitrary data to Eloquent models.
Features
- One-to-many polymorphic relationship allows attaching data to Eloquent models without needing to adjust the database schema.
- Type conversion system allows data of numerous different scalar and object types to be stored and retrieved. See the documentation for the list of supported types.
Example Usage
Attach some metadata to an eloquent model
$post = Post::create($this->request->input()); $post->setMeta('color', 'blue');
Query the model by its metadata
$post = Post::whereMeta('color', 'blue');
Retrieve the metadata from a model
$value = $post->getMeta('color');
Installation
- Add the package to your Laravel app using composer
composer require plank/laravel-metable
- Register the package's service provider in
config/app.php
. In Laravel versions 5.5 and beyond, this step can be skipped if package auto-discovery is enabled.
'providers' => [ ... Plank\Metable\MetableServiceProvider::class, ... ];
- Publish the config file (
config/metable.php
) of the package using artisan.
php artisan vendor:publish --provider="Plank\Metable\MetableServiceProvider"
- Run the migrations to add the required table to your database.
php artisan migrate
- Add the
Plank\Metable\Metable
trait to any eloquent model class that you want to be able to attach metadata to.
Note: If need a more generic way to reference different Metable Model classes, you can optionally apply the Plank\Metable\MetableInterface
to your models.
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Plank\Metable\Metable; class Post extends Model { use Metable; //... }
Documentation
The full documentation is available on ReadTheDocs.
License
This package is released under the MIT license (MIT).
About Plank
Plank is a web development agency based in Montreal, Canada.