PHP code example of patoui / model-history

1. Go to this page and download the library: Download patoui/model-history 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/ */

    

patoui / model-history example snippets


'model' => \App\Models\ModelHistory::class,

'exclude_properties' => [
    'id',
    'created_at',
    'updated_at',
    'deleted_at',
    'password',
    'remember_token',
    '_token',
],

protected array $modelHistoryExcludedProperties = ['password'];

use Illuminate\Database\Eloquent\Model;
use Patoui\ModelHistory\Contracts\TrackModelHistoryContract;
use Patoui\ModelHistory\Traits\TrackModelHistory;

class User extends Model implements TrackModelHistoryContract
{
    use TrackModelHistory;
}

// Creation is tracked
$user = User::create([
    'name' => 'John Doe',
    'email' => '[email protected]'
]);

// Updates are tracked
$user->update(['name' => 'Jane Doe']);

$this->app->singleton(ModelHistoryRepositoryContract::class, MyNewChangeRepository::class);



declare(strict_types=1);

use PhpClickHouseLaravel\RawColumn;
use PhpClickHouseLaravel\Migration;
use PhpClickHouseSchemaBuilder\Tables\MergeTree;

return new class extends Migration
{
    public function up(): void
    {
        static::createMergeTree('model_histories', fn (MergeTree $table) => $table
            ->columns([
                $table->uuid('id')->default(new RawColumn('generateUUIDv4()')),
                $table->string('model_type'),
                $table->string('model_id'),
                $table->string('old'),
                $table->string('new'),
                $table->uInt64('auth_id'),
                $table->datetime('created_at', 3),
            ])->orderBy('created_at')
        );
    }

    public function down(): void
    {
        static::write('DROP TABLE model_histories');
    }
};



declare(strict_types=1);

namespace App\Models;

use PhpClickHouseLaravel\BaseModel;

class ModelHistory extends BaseModel
{
    protected $casts = [
        'old' => 'array',
        'new' => 'array',
    ];
}
bash
php artisan vendor:publish --tag="model-history-config"
bash
php artisan vendor:publish --tag="model-history-migrations"
php artisan migrate
bash
php artisan vendor:publish --provider="Patoui\ModelHistory\ModelHistoryServiceProvider"

php artisan vendor:publish --tag="model-history-config"