1. Go to this page and download the library: Download panoscape/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/ */
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Panoscape\History\HasOperations;
class User extends Authenticatable
{
use Notifiable, SoftDeletes, HasOperations;
}
namespace App;
use Illuminate\Database\Eloquent\Model;
use Panoscape\History\HasHistories;
class Article extends Model
{
use HasHistories;
public function getModelLabel()
{
return $this->display_name;
}
}
// get the lastest 10 records
$model->histories()->orderBy('performed_at', 'desc')->take(10)
// filter by user id
$model->histories()->where('user_id', 10010)
//get the associated model
$history->model();
//get the associated user
//the user is the authenticated user when the action is being performed
//it might be null if the history is performed unauthenticatedly
$history->user();
//check user existence
$history->hasUser();
//get the message
$history->message;
//get the meta(only available when it's an updating operation)
//the meta will be an array with the properties changing information
$history->meta;
//get the timestamp the action was performed at
$history->performed_at;
use Panoscape\History\Events\ModelChanged;
...
//fire a model changed event
event(new ModelChanged($user, 'User roles updated', $user->roles()->pluck('id')->toArray()));
/*
|--------------------------------------------------------------------------
| Tracker Language Lines
|--------------------------------------------------------------------------
|
| The following language lines are used across application for various
| messages that we need to display to the user. You are free to modify
| these language lines according to your application's
// if you specified the translation key, the message argument will be ignored, simply just pass `null`
event(new ModelChanged($user, null, $user->roles()->pluck('id')->toArray()), 'switched_role');
/*
|--------------------------------------------------------------
| Events whitelist
|--------------------------------------------------------------
|
| Events in this array will be recorded.
| Available events are: created, updating, deleting, restored
|
*/
'events_whitelist' => [
'created', 'updating', 'deleting', 'restored',
],
/*
|--------------------------------------------------------------
| Attributes blacklist
|--------------------------------------------------------------
|
| Please add the whole class names. Example: \App\User:class
| For each model, attributes in its respect array will NOT be recorded into meta when performing update operation.
|
*/
'attributes_blacklist' => [
// \App\User::class => [
// 'password'
// ],
],
/*
|--------------------------------------------------------------
| User type blacklist
|--------------------------------------------------------------
|
| Operations performed by user types in this array will NOT be recorded.
| Please add the whole class names. Example: \App\Admin:class
| Use 'nobody' to bypass unauthenticated operations
|
*/
'user_blacklist' => [
// \App\Admin:class,
// 'nobody'
],
/*
|--------------------------------------------------------------
| Enviroments blacklist
|--------------------------------------------------------------
|
| When application's environment is in the list, tracker will be disabled
|
*/
'env_blacklist' => [
// 'test'
],
/*
|--------------------------------------------------------------
| Enable auth guards scanning
|--------------------------------------------------------------
|
| You only need to enable this if your users are using non-default auth guards.
| In that case, all tracked user operations will be anonymous.
|
| - Set to `true` to use a full scan mode: all auth guards will be checked. However this does not ensure guard priority.
| - Set to an array to scan only specific auth guards(in the given order). e.g. `['web', 'api', 'admin']`
|
*/
'auth_guards' => null
class Article extends Model
{
// if you want to use default trait method, you need to redeclare it with a new name
use HasHistories {
getModelMeta as protected traitGetModelMeta;
};
...
public function getModelMeta($event)
{
// using defaults for updating
if($event == 'updating') return $this->traitGetModelMeta($event);
// passing full model to meta
// ['key1' => 'value1', 'key2' => 'value2', ...]
else return $this;
}
}
class Article extends Model
{
use HasHistories;
public function getModelLabel()
{
return $this->title;
}
}
// original title is 'my title'
// modify title
$article->title = 'new title';
$article->save();
// the updating history message
// expect: Updating Article my title
// actual: Updating Article new title
public function getModelLabel()
{
return $this->getOriginal('title', $this->title);
}