laravel-properties maintained by tobymaxham
TobyMaxham Laravel Properties
This package can be used to add some functionality to your Eloquent Models using properties.
Installation
You can install the package via composer:
composer require tobymaxham/laravel-properties
Usage
Your 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');
}
};
Use to store values in your Model
$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');
Receive a value from your Model
$model->getProperty('foo.bar'); // 'value'
You can also pass a default value:
$model->getProperty('foo.baz', 'another Value'); // 'another Value'
Change the database field
By default, the database field properties will be used.
You can change this behavoi by add the name with field should be used.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use TobyMaxham\LaravelProperties\Traits\UseProperties;
class YourEloquentModel extends Model
{
use UseProperties;
protected string $propertiesField = 'properties';
}
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
If you've found a bug regarding security, please mail git@maxham.de instead of using the issue tracker.
Support me
Credits
License
The MIT License (MIT). Please see License File for more information.