davidmpeace/immutability
Composer 安装命令:
composer require davidmpeace/immutability
包简介
A simple Laravel package that allows you to enforce immutable attributes on Eloquent models.
README 文档
README
Immutability is a simple package that is used in your Eloquent models to enforce attribute immutability. Immutable attributes may be set, and changed once, but once the model is saved, the value can not be changed.
License
Immutability is open-sourced software licensed under the MIT license
Installation
To get started with Immutability, add to your composer.json file as a dependency:
composer require davidmpeace/immutability
Basic Usage
To use the Immutability library, you simply need to use the Immutability trait for any model you wish to identify immutable attributes for. Typically, you would want to implement the trait in your super-class so that all your sub-classes will automatically inherit the functionality.
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Eloquent\Attributes\Immutability; class MyAppSuperModel extends Model { use Immutability; protected $immutable = ['id', 'uuid']; }
Class override.
<?php namespace App; class User extends MyAppSuperModel { protected $immutable = ['id', 'uuid', 'username']; }
Catching exceptions
<?php namespace App; use Eloquent\Attributes\Exceptions\ImmutableFieldViolationException; use Exception; $user = User::find(1); try { $user->username = "myNewUserName"; $user->save(); } catch ( ImmutableFieldViolationException $e ) { // Handle immutable attribute violation error } catch ( Exception $e ) { // Handle error }
统计信息
- 总下载量: 20.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-22