2017-02-08 12:51:15 -08:00
|
|
|
# Laravel 5+ Read Only Models
|
|
|
|
The read only trait removes the ability to save, delete or modify Laravel models.
|
|
|
|
Ideally, this would be used in addition to DB permissions to ensure users and developers cannot write to a Legacy system.
|
|
|
|
|
|
|
|
## Install
|
|
|
|
|
|
|
|
```
|
|
|
|
composer require michaelachrisco/readonly
|
|
|
|
```
|
2016-03-28 11:26:43 -07:00
|
|
|
|
|
|
|
## To use:
|
|
|
|
|
2017-02-08 12:51:15 -08:00
|
|
|
|
|
|
|
|
2016-03-28 11:26:43 -07:00
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-03-27 13:14:50 -07:00
|
|
|
use MichaelAChrisco\ReadOnly\ReadOnlyTrait;
|
2016-12-06 11:16:18 -08:00
|
|
|
class User extends Model {
|
2017-03-27 13:14:50 -07:00
|
|
|
use ReadOnlyTrait;
|
2016-03-28 11:26:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$legacyUser = new User;
|
|
|
|
$legacyUser->set_user_name('bob');
|
2016-12-06 11:16:18 -08:00
|
|
|
|
2016-03-28 11:26:43 -07:00
|
|
|
$result = $legacyUser->save();
|
2017-03-27 13:14:50 -07:00
|
|
|
//User is not saved and ReadOnlyException is thrown.
|
2016-03-28 11:26:43 -07:00
|
|
|
?>
|
|
|
|
```
|
2016-12-06 11:16:18 -08:00
|
|
|
|
2017-03-27 13:14:50 -07:00
|
|
|
## Methods that will throw ReadOnlyExceptions:
|
2016-12-06 11:16:18 -08:00
|
|
|
|
|
|
|
* create
|
|
|
|
* forceCreate
|
|
|
|
* save
|
|
|
|
* update
|
|
|
|
* firstOrCreate
|
|
|
|
* firstOrNew
|
|
|
|
* delete
|
|
|
|
* destroy
|
|
|
|
* restore
|
|
|
|
* forceDelete
|
2016-12-09 06:37:13 -08:00
|
|
|
* performDeleteOnModel
|
|
|
|
* push
|
|
|
|
* finishSave
|
|
|
|
* performUpdate
|
|
|
|
* touch
|
2017-03-29 10:15:48 -07:00
|
|
|
* insert
|
|
|
|
* truncate
|
2016-12-06 11:16:18 -08:00
|
|
|
|
|
|
|
## TODO:
|
|
|
|
* saveOrFail
|
|
|
|
* performInsert(??)
|
|
|
|
* insertAndSetId(??)
|
|
|
|
* Add in a PR for any other methods you can find!
|
|
|
|
|
|
|
|
|
2017-08-10 11:09:03 -07:00
|
|
|
### registerModelEvents (look into best way to implement)
|
2016-12-06 11:16:18 -08:00
|
|
|
* saving
|
|
|
|
* saved
|
|
|
|
* updating
|
|
|
|
* updated
|
|
|
|
* creating
|
|
|
|
* created
|
|
|
|
* deleting
|
|
|
|
* deleted
|
|
|
|
|
|
|
|
###
|