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;
|
2016-12-06 11:16:18 -08:00
|
|
|
class User extends Model {
|
2017-02-08 12:51:15 -08:00
|
|
|
use \MichaelAChrisco\ReadOnly\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();
|
|
|
|
//User is not saved and $result is false.
|
|
|
|
?>
|
|
|
|
```
|
2016-12-06 11:16:18 -08:00
|
|
|
|
|
|
|
## Methods that will return false:
|
|
|
|
|
|
|
|
* create
|
|
|
|
* forceCreate
|
|
|
|
* save
|
|
|
|
* update
|
|
|
|
* firstOrCreate
|
|
|
|
* firstOrNew
|
|
|
|
* delete
|
|
|
|
* destroy
|
|
|
|
* restore
|
|
|
|
* forceDelete
|
2016-12-09 06:37:13 -08:00
|
|
|
* performDeleteOnModel
|
|
|
|
* push
|
|
|
|
* finishSave
|
|
|
|
* performUpdate
|
|
|
|
* touch
|
2016-12-06 11:16:18 -08:00
|
|
|
|
|
|
|
## TODO:
|
|
|
|
* saveOrFail
|
|
|
|
* performInsert(??)
|
|
|
|
* insertAndSetId(??)
|
|
|
|
* Add in a PR for any other methods you can find!
|
|
|
|
|
|
|
|
|
|
|
|
### registerModelEvents( look into best way to implement)
|
|
|
|
* saving
|
|
|
|
* saved
|
|
|
|
* updating
|
|
|
|
* updated
|
|
|
|
* creating
|
|
|
|
* created
|
|
|
|
* deleting
|
|
|
|
* deleted
|
|
|
|
|
|
|
|
###
|