This package can be used to add some functionality to your Eloquent Models using properties.
You can install the package via composer:
composer require tobymaxham/laravel-propertiesYour Eloquent Models should use the TobyMaxham\LaravelProperties\Traits\UseProperties trait.
The trait contains a few methods to help you handle JSON-Date in your Database Table Column.
Your models' migrations should have a field called properties to save the JSON-Data.
Here's an example of how to implement the trait:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use TobyMaxham\LaravelProperties\Traits\UseProperties;
class YourEloquentModel extends Model
{
use UseProperties;
}With its migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('your_eloquent_models', function (Blueprint $table) {
$table->id();
$table->json('properties');
// ...
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('your_eloquent_models');
}
};$model = new EloquentModel();
$model->setProperty('key', 'value');
$model->save();You can also use Laravel's "dot" notation to set a value:
$model->setProperty('foo.bar', 'value');$model->getProperty('foo.bar'); // 'value'You can also pass a default value:
$model->getProperty('foo.baz', 'another Value'); // 'another Value'Please see CHANGELOG for more information on what has changed recently.
If you've found a bug regarding security please mail git@maxham.de instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.