PHP code example of ttpn18121996 / historical-records

1. Go to this page and download the library: Download ttpn18121996/historical-records 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/ */

    

ttpn18121996 / historical-records example snippets


use Illuminate\Foundation\Auth\User as Authenticatable;
use HistoricalRecords\Concerns\HasHistory;
use HistoricalRecords\Contracts\Historyable;

class User extends Authenticatable implements Historyable
{
    use HasHistory;

    //...
}


use HistoricalRecords\HistoryManager;

/*
id: 1
name: Trinh Tran Phuong Nam
email: [email protected]
*/
$user = auth()->user();

$history = HistoryManager::save(
    historyable: $user,
    feature: 'users',
    keyword: 'create',
    payload: ['id' => 2, 'name' => 'Minh Nguyet', 'email' => '[email protected]'],
);

echo sprintf(__('history.'.$history->feature.'.'.$history->keyword.'.action'), $user->name);
// Trinh Tran Phuong Nam has created a user.

use App\Models\History;
use HistoricalRecords\HistoryManager;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        HistoryManager::$modelName = History::class;
    }
}

return [
    'users' => [
        'create' => [
            'title' => 'Create',
            'action' => '%s has created a user.',
        ],
        'update' => [
            'title' => 'Update',
            'action' => '%s has updated a user.',
        ],
        'delete' => [
            'title' => 'Delete',
            'action' => '%s has deleted a user.',
        ],
        'destroy' => [
            'title' => 'Force delete',
            'action' => '%s has hard deleted a user.',
        ],
        'restore' => [
            'title' => 'Restore',
            'action' => '%s has restored a user.',
        ],
        'login' => [
            'title' => 'Login',
            'action' => '%s has logged in.',
        ],
        'change_password' => [
            'title' => 'Change password',
            'action' => '%s has changed the login account password.',
        ],
        'update_profile' => [
            'title' => 'Update profile',
            'action' => '%s has updated the profile.',
        ],
        'email_verification' => [
            'title' => 'Email verification',
            'action' => '%s has verified the email.',
        ],
    ],
];

/*
User [
    'id' => 1
    'name' => 'John Doe'
]
History [
    'feature' => 'users',
    'ketword' => 'create'
    'historyable_id' => 1,
    'historyable_type' => App\Models\User,
]
*/
$history = History::first();

echo $history->action_for_trans;

// historical.users.create.action
// :name has created a user.

__($history->action_for_trans, ['name' => $history->user->name]);

// John Doe has created a user

return [
    'history_expires' => 90, // days
    ...
];

return [
    ...
    'device_name' => [ // device name that will be saved
        'phone' => 'phone',
        'tablet' => 'tablet',
        'desktop' => 'desktop',
        'default' => 'unknown',
    ],
];
bash
php artisan historical-records:install
bash
php artisan historical-records:cleanup
bash
php artisan historical-records:cleanup --time=14days
#OR
php artisan historical-records:cleanup -t 14d