Laravel package for handling metadata for all your models.
danielefavi/laravel-metadata is a Laravel package for laravel package for handling metadata for all your models..
It currently has 4 GitHub stars and 67 downloads on Packagist (latest version 1.3).
Install it with composer require danielefavi/laravel-metadata.
Discover more Laravel packages by danielefavi
or browse all Laravel packages to compare alternatives.
Last updated
Laravel Metadata let you store extra data using your model without adding any extra field to your model.
$user->saveMeta('age', 25); // storing
$user->saveMeta('dog_name', 'Buddy'); // storing
$age = $user->getMeta('age');
Install the package via composer:
composer require danielefavi/laravel-metadata
Then add in the config/app.php file the entry DanieleFavi\Metadata\MetaServiceProvider::class, in the providers section:
'providers' => [
// ...
DanieleFavi\Metadata\MetaServiceProvider::class,
],
Then run the migration:
php artisan migrate
After installing the package you have to add the trait HasMetadata in your model.
For example
use DanieleFavi\Metadata\HasMetadata; // to add in your model
class User extends Authenticatable
{
use HasFactory;
use HasMetadata; // to add in your model
// ...
}
Now your model has all the methods to handle the metadata.
Using the method saveMeta you can save (store or update) a metadata:
$user->saveMeta('phone_number', '111222333'); // storing a value
$user->saveMeta('color_preference', ['orange', 'yellow']); // storing an array
With saveMetas you can save multiple metadata at once:
$user->saveMetas([
'phone_number' => '333222111',
'color_preference' => ['red', 'green'],
'address' => '29 Arlington Avenue',
]);
The method getMeta retrieve the metadata for the given key:
$phoneNumber = $user->getMeta('phone_number'); // the value of $phoneNumber is '111222333'
// Default value in case the metadata has not been found (default: null)
$anotherMeta = $user->getMeta('another_meta', 10);
You can retrieve the metadata in bulk
// return an array key => value with the metadata specified for the given keys
$metas = $user->getMetas(['phone_number', 'address']);
// the value of the $metas is an array key => value:
// [
// 'phone_number' => '111222333',
// 'address' => '29 Arlington Avenue'
// ]
// return an array key => value containing all metadata of the user model
$metas = $user->getMetas();
If you need to get the metadata object (and not just the value as getMeta and getMetas) you can use the method getMetaObj:
$metaObj = $user->getMetaObj('address');
// delete a single metadata
$user->deleteMeta('address');
// delete multiple metadata
$user->deleteMeta(['phone_number', 'address']);
// delete all metadata
$user->deleteAllMeta();
Getting the collection of metadata attached to the model:
$list = $user->metas;
Getting all the users that have the hair color brown or pink.
$users = User::metaWhere('hair_color', 'brown')
->orMetaWhere('hair_color', 'pink')
->get();
Getting all the users with the hair color brown or pink and with a dog named Charlie:
$users = User::metaWhere('dog_name', 'charlie')
->where(function($query) {
return $query->metaWhere('hair_color', 'brown')
->orMetaWhere('hair_color', 'pink');
})
->get();
You can query the metadata using has, whereHas and with. For example:
$users = User::whereHas('metas', function($query) {
$query->where('key', 'hair_color');
$query->where('value', json_encode('blue')); // remember to json_encode the value!!!
})->get();
Important: when doing your custom queries remember to JSON encode the metavalue because in the DB the metavalue is stored as JSON.
In the example above
Getting all the users with all their metadata:
$users = User::with('metas')->get();
Or lazy loading the metadata:
$user = User::find(1);
$user->load('metas');