zablockibros / laravel-immutable
Make model attributes immutable
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 1
pkg:composer/zablockibros/laravel-immutable
Requires
- php: ^7.1.3
- illuminate/database: ~5.7.0
- illuminate/support: ~5.7.0
Requires (Dev)
- fzaninotto/faker: ^1.4
- mockery/mockery: ^1.0
- phpunit/phpunit: ~7.0
This package is auto-updated.
Last update: 2025-10-05 21:42:57 UTC
README
Make attributes on your Laravel models immutable...i.e. after the model is created, the value of the immutable attributes cannot change when updating records with Eloquent.
Installation
Requirements: This package requires PHP 7.1.3 or higher and Laravel 5.7
$ composer require zablockibros/laravel-immutable
The package will automatically register its service provider.
Define Immutable Attributes
Define the attributes to be immutable on your model:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use ZablockiBros\Immutable\Traits\HasImmutableAttributes; class YourModel extends Model { use HasImmutableAttributes; /** * @var array */ protected $immutable = [ 'name', 'sku', ];
In this example, name
and sku
can be set on model creation, however, the attributes will not persist changes of their value to a database on update.
$model = new YourModel; // set the attribute $model->name = 'test'; $model->name; // 'test' // change it (pre-saving) $model->name = 'changed'; $model->name; // 'changed' // now we save the model $model->save(); // can't change its value now $model->name = 'nope'; $model->name; // 'changed' // can't update it either $model->save([ 'name' => 'nope', ]); $model->name; // 'changed'
Copyright and License
Copyright (c) 2019 Justin Zablocki