๐Ÿ‹๏ธ
Lift for Laravel
View on GitHub
  • ๐Ÿ‹๏ธLift for Laravel
  • ๐Ÿš€Getting Started
    • Installation
    • Changelog
  • ๐Ÿ› ๏ธAttributes
    • Cast
    • Column
    • Config
    • DB
    • Events
    • Fillable
    • Hidden
    • Ignore Properties
    • Immutable
    • Primary Key
    • Relationships
    • Validation
    • Watch
  • ๐ŸงชMethods
    • castAndCreate
    • castAndFill
    • castAndSet
    • castAndUpdate
    • createValidationMessages
    • createValidationRules
    • customColumns
    • defaultValues
    • immutableProperties
    • updateValidationMessages
    • updateValidationRules
    • validationMessages
    • validationRules
    • watchedProperties
  • ๐Ÿค–Commands
    • lift:migration
Powered by GitBook
On this page

Was this helpful?

  1. Attributes

Immutable

The Immutable attribute allows you to set your model's public properties as immutable. This means that once the model is created, the public properties will not be able to be changed. If you try to change the value of an immutable property an WendellAdriel\Lift\Exceptions\ImmutablePropertyException will be thrown.

use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Model;
use WendellAdriel\Lift\Attributes\Cast;
use WendellAdriel\Lift\Attributes\Fillable;
use WendellAdriel\Lift\Attributes\Immutable;
use WendellAdriel\Lift\Lift;

final class Product extends Model
{
    use Lift;

    #[Immutable]
    #[Fillable]
    public string $name;

    #[Fillable]
    #[Cast('float')]
    public float $price;
}

Example:

$product = Product::create([
    'name' => 'Product Name',
    'price' => 10.0,
]);

$product->name = 'New Product Name';
$product->save(); // Will throw an ImmutablePropertyException
PreviousIgnore PropertiesNextPrimary Key

Last updated 1 year ago

Was this helpful?

๐Ÿ› ๏ธ