novocast / laravel-readonly
A Read-only model trait for assisting dealign with read only models.
dev-master
2022-05-16 12:52 UTC
Requires
- php: >=7.0.0
This package is auto-updated.
Last update: 2025-04-16 19:13:28 UTC
README
Simple Trait
Install
composer require novocast/laravel-readonly
Using the Trait:
It's really easy. You add the trait. If you attempt to use a non read-only method, it will throw an exception.
<?php use Illuminate\Database\Eloquent\Model; use Novocast\ReadOnly\ReadOnlyTrait; class ReadOnlyModel extends Model { use ReadOnlyTrait; } $readme = new ReadOnlyModel(); /** * When calling this save method, the ReadOnlyExceptoin Exception * is thrown */ $readme->save();
You may choose to handle the exception if you have a mix of objects that can and can't be saved in a collection. Consider the following:
<?php use Illuminate\Database\Eloquent\Model; use Novocast\ReadOnly\ReadOnlyTrait; use Novocast\ReadOnly\ReadOnlyException; class ReadOnlyModel extends Model { use ReadOnlyTrait; } class RegularModel extends Model { // not using the read only trait } $models = []; for($count = 0; $count < 5; $count++) { if (mt_rand(0,100) > 50) { $models[] = new RegularModel(); } if (mt_rand(0,100) > 40) { $models[] = new ReadOnlyModel(); } } $collection = collect($models); foreach($models as $model) { try { $model->save(); } catch(ReadOnlyException $exception) { // This is a read only model. } } ?>