latevaweb / laravel-custom-properties
Laravel trait to make add dynamic properties to Eloquent models
Installs: 1 267
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 2
Open Issues: 3
Type:laravel-package
Requires
- php: ^7.3|^8.2|^8.3
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^4.0|^5.0
- phpunit/phpunit: ^7.0|^8.0|^9.0
This package is auto-updated.
Last update: 2025-03-05 11:28:00 UTC
README
Trait to add dynamic custom properties to Eloquent models
Once the trait is installed on the model and migration field is added you can do these things:
$customer = new Customer; // An Eloquent model $customer ->setCustomProperty('foo', 'bar') ->setCustomProperty('foo2', 'bar2') ->save(); $customer->hasCustomProperty('foo'); // Returns 'true' $customer->getCustomProperty('foo'); // returns 'bar' $customer->forgetCustomProperty('foo'); // removes field 'foo' from model array // Don't forget to persist it! $customer->save();
Installation
You can install the package via composer:
composer require latevaweb/laravel-custom-properties
Adding custom properties to a Model
The required steps to add custom properties to a model are:
- First, you need to add the
LaTevaWeb\CustomProperties\HasCustomProperties
-trait. - It is required to add to your model table the field
$table->json('custom_properties')->nullable();
Here's an example of a prepared model:
use Illuminate\Database\Eloquent\Model; use LaTevaWeb\CustomProperties\HasCustomProperties; class NewsItem extends Model { use HasCustomProperties; }
And the migration:
Schema::table('your_table', function (Blueprint $table) { $table->json('custom_properties')->nullable(); });