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
|
2018-04-24 15:02:43 -07:00
|
|
|
* Add in a PR for any other methods you can find!
|
2016-12-06 11:16:18 -08:00
|
|
|
|
|
|
|
###
|