This package make it possible to manage a json meta column using dot notation.
composer require paulhenri-l/laravel-has-meta
In your migrations add a meta
column with a type of json
Schema::create('users', function ($table) {
$table->bigIncrements('id');
$table->json('meta');
$table->timestamps();
});
Now inside your model use the HasMeta
trait.
<?php
namespace PaulhenriL\LaravelHasMeta\Tests\Fakes;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use \PaulhenriL\LaravelHasMeta\HasMeta;
}
You can now use the meta API. The get and set methods use the dot notation in order to get and set values in nested arrays.
The encrypted methods uses the Crypt
facade and will therefore use your app's
encryption settings.
$user = new User();
// Set
$user->setMeta('preferences.time_zone', 'Europe/Paris');
$user->setEncryptedMeta('health.has_diabet', true);
// Get
$user->getMeta('preferences.time_zone');
$user->getEncryptedMeta('health.has_diabet');