PHP code example of isaevdimka / laravel-model-changes-history

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

    

isaevdimka / laravel-model-changes-history example snippets


use ModelChangesHistory\Traits\HasChangesHistory;
use Illuminate\Database\Eloquent\Model;

class TestModel extends Model {
    use HasChangesHistory;

    /**
     * The attributes that are mass assignable.
     * This will also be hidden for changes history.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];
}


$testModel->latestChange();

ModelChangesHistory\Models\Change {
    ...
    #attributes:  [
        "model_id" => 1
        "model_type" => "App\TestModel"
        "before_changes" => "{...}"
        "after_changes" => "{...}"
        "change_type" => "updated"
        "changes" => "{
            "title": {
                "before": "Some old title",  
                "after": "This is the new title"
            },
            "body": {
                "before": "Some old body",  
                "after": "This is the new body"
            },
            "password": {
                "before": "[hidden]",  
                "after": "[hidden]"
            }
        }"
        "changer_type" =>  "App\User"
        "changer_id" => 1
        "stack_trace" => "{...}"
        "created_at" => "2020-01-21 17:34:31"
    ]
    ...
}

$testModel->historyChanges();

Illuminate\Database\Eloquent\Collection {
  #items: array:3 [
    0 => ModelChangesHistory\Models\Change {...}
    1 => ModelChangesHistory\Models\Change {...}
    2 => ModelChangesHistory\Models\Change {...}
    ...
}

$testModel->latestChangeMorph();
$testModel->historyChangesMorph();

$testModel->clearHistoryChanges();

use ModelChangesHistory\Facades\HistoryStorage;
...

$latestChanges = HistoryStorage::getHistoryChanges(); // Return collection of all latest changes
$latestChanges = HistoryStorage::getHistoryChanges($testModel); // Return collection of all latest changes for model

$latestChange = HistoryStorage::getLatestChange(); // Return latest change
$latestChange = HistoryStorage::getLatestChange($testModel); // Return latest change for model

HistoryStorage::deleteHistoryChanges(); // This will delete all history changes
HistoryStorage::deleteHistoryChanges($testModel); // This will delete all history changes for model

// Return Authenticatable `changer_type` using HasOne relation to changer_type and changer_id
$changer = $latestChange->changer; 

use ModelChangesHistory\Models\Change;

// Get the updates on the given model, by the given user, in the last 30 days:
Change::query()
    ->whereModel($testModel)
    ->whereChanger($user)
    ->whereType(Change::TYPE_UPDATED)
    ->whereCreatedBetween(now()->subDays(30), now())
    ->get();

protected function schedule(Schedule $schedule)
{
    $schedule->command('changes-history:clear')->monthly();
}
bash
php artisan vendor:publish --tag="model-changes-history"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="ModelChangesHistory\Providers\ModelChangesHistoryServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="ModelChangesHistory\Providers\ModelChangesHistoryServiceProvider" --tag="migrations"
bash
php artisan migrate
bash
php artisan changes-history:clear