PHP code example of hareland / laravel-immutable-attributes

1. Go to this page and download the library: Download hareland/laravel-immutable-attributes 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/ */

    

hareland / laravel-immutable-attributes example snippets



 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Hareland\LaravelImmutableAttributes\Traits\HasImmutableAttributes;
 
class Product extends Model
{
    use HasImmutableAttributes;
    
    /**
     * @var array
     */
    protected $immutable = [
        'label',
        'price',
    ];

$model = new Product;
 
// Set the attribute 
$model->label = 'abc';
$model->label; // 'abc'
 
// Change it (before-saving)
$model->label = 'abc';
$model->label; // 'abc'
 
// Save it
$model->save();
 
// You can't change its value
$model->label = 'xyz';
$model->label; // 'abc'
 
// You can't update it either
$model->save([
    'label' => 'xyz',
]);
$model->label; // 'abc'