michaelachrisco / ReadOnlyTraitLaravel by michaelachrisco

Readonly Models for Laravel 5+
1,166,077
135
5
Package Data
Maintainer Username: michaelachrisco
Maintainer Contact: michaelachrisco@gmail.com (Michael Chrisco)
Package Create Date: 2016-03-28
Package Last Update: 2024-03-27
Home Page: https://github.com/michaelachrisco/ReadOnlyTraitLaravel
Language: PHP
License: MIT
Last Refreshed: 2024-04-16 15:03:10
Package Statistics
Total Downloads: 1,166,077
Monthly Downloads: 36,230
Daily Downloads: 2,041
Total Stars: 135
Total Watchers: 5
Total Forks: 14
Total Open Issues: 2

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!