Skip to content

Metadata trait ΒΆ

Small trait available to models to add associated metadata, json encoded.

Add a migration to add a ‘text’ field called ‘metadata’

Schema::table('myTable', function (Blueprint $table) {
    $table->text('metadata')->nullable();
});

Use the HasMetadata trait in your model

use App\Models\Traits\HasMetadata;

You can now add miscellaneous metadata for later use.

$address = Address::find($id);
$address->setMeta('zip_plus_4', '27596-8392');
$address->save();
...
...
$newAddress = Address::find($id);
echo $address->getMeta('zip_plus_4');

MetadataTest.php included

Note this was not done with a ‘$casts’ attribute on the model because it might conflict with other existing explicit $casts attributes.