Download the PHP package splitstack/readonly-models without Composer
On this page you can find all versions of the php package splitstack/readonly-models. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download splitstack/readonly-models
More information about splitstack/readonly-models
Files in splitstack/readonly-models
Package readonly-models
Short Description Block Eloquent persistence on read-only / value-object models.
License MIT
Informations about the package readonly-models
readonly-models
Block persistence on Eloquent models. Drop the PreventsWrites trait onto a
model and every write path (save, update, delete, forceDelete) throws a
ReadOnlyException instead of touching the database.
Useful for value objects, in-memory entities, database views, and any model that should never be written back.
Installation
Requires PHP 8.2+ and illuminate/database 10 through 13. It only needs the
database component, not the full framework, so it works inside a Laravel app or
standalone with the Eloquent Capsule.
Usage
Because create(), saveQuietly() and push() all route through save(),
and the static destroy() routes through delete(), those four overrides cover
the whole write surface.
Catching the exception
ReadOnlyException extends BadMethodCallException (itself a LogicException),
so you can catch it at whichever level fits:
Customising the behaviour
Keep one write method working. A method declared directly on the class
always wins over the trait's version, so you can carve out an exception without
any trait conflict. For example, route update() through another model:
Change the exception or message. Override the protected denyWrite() hook.
It receives the blocked method name and returns the exception to throw:
MyReadOnlyException just needs to extend ReadOnlyException.
License
MIT. See LICENSE.