<?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"
]
...
}
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();
}