1. Go to this page and download the library: Download splitstack/readonly-models library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
splitstack / readonly-models example snippets
use Illuminate\Database\Eloquent\Model;
use Splitstack\ReadonlyModels\Concerns\PreventsWrites;
class ExchangeRate extends Model
{
use PreventsWrites;
}
$rate = ExchangeRate::find(1); // reads work as normal
$rate->rate = 1.23;
$rate->save(); // throws ReadOnlyException
class ViewModel extends Model
{
use PreventsWrites;
public function update(array $attributes = [], array $options = [])
{
// your own logic — save/delete/forceDelete stay blocked
}
}
use Splitstack\ReadonlyModels\Exceptions\ReadOnlyException;
protected function denyWrite(string $method): ReadOnlyException
{
return new MyReadOnlyException(match ($method) {
'save' => 'This record is immutable.',
'delete' => 'This record cannot be removed.',
default => "Cannot {$method} this record.",
});
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.