Laravel Read Only Model Traits
Find a file
Simon Bigelmayr dca9d5bef5
feat: add possibility to disable the trait programmatically (#43)
* feat: add possibility to disable the trait programmatically

* test: add test draft

* Attempt at getting a workable test.

* Test ugly mockable solution for happy path

Co-authored-by: Simon Bigelmayr <simon.bigelmayr@mll.com>
Co-authored-by: Michael chrisco <michaelachrisco@gmail.com>
2022-09-07 09:14:13 -07:00
.circleci fix(composer): Update illuminate database to fix ci issues (#42) 2022-08-22 09:49:47 -07:00
spec feat: add possibility to disable the trait programmatically (#43) 2022-09-07 09:14:13 -07:00
src feat: add possibility to disable the trait programmatically (#43) 2022-09-07 09:14:13 -07:00
.gitignore Add PHP-CS-Fixer to linting process and Travis. 2017-03-31 11:40:47 -07:00
composer.json fix(composer): Update illuminate database to fix ci issues (#42) 2022-08-22 09:49:47 -07:00
LICENSE License (#13) 2017-03-23 09:34:11 -07:00
README.md Update README.md 2018-06-05 08:36:56 -07: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

To use:

<?php
use Illuminate\Database\Eloquent\Model;
use MichaelAChrisco\ReadOnly\ReadOnlyTrait;
class User extends Model {
  use ReadOnlyTrait;
}

$legacyUser = new User;
$legacyUser->set_user_name('bob');

$result = $legacyUser->save();
//User is not saved. 
//ReadOnlyException is thrown.
 ?>

Methods that will throw ReadOnlyExceptions:

  • create
  • forceCreate
  • save
  • update
  • firstOrCreate
  • firstOrNew
  • delete
  • destroy
  • restore
  • forceDelete
  • performDeleteOnModel
  • push
  • finishSave
  • performUpdate
  • touch
  • insert
  • truncate
  • Add in a PR for any other methods you can find!