PHP code example of patrixsmart / adjustfly

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

    

patrixsmart / adjustfly example snippets


use Illuminate\Database\Eloquent\Model;
use Patrixsmart\Adjustfly\Concerns\HasAdjustments;

class Student extends Model
{
    use HasAdjustments;
}

$student->update(['name' => 'Ada Lovelace']);

$student->adjustments()->first();
// event:  "updated"
// before: ['name' => 'Alan Turing']
// after:  ['name' => 'Ada Lovelace']
// user_id, ip_address, user_agent, created_at

// config/adjustfly.php
'events' => ['created', 'updating', 'deleted', 'restored'],

$adjustment = $student->recordAdjustment();   // returns null when nothing changed

'excluded_attributes' => ['password', 'remember_token', 'updated_at', 'created_at', 'deleted_at'],

class Student extends Model
{
    use HasAdjustments;

    protected array $adjustmentExcluded = ['internal_notes'];

    // ...or record nothing but these:
    protected array $adjustmentOnly = ['name', 'email', 'class_id'];
}

$student->update(['class_id' => 4]);

$student->adjustments()->first()->adjustable_label;   // "Ada Lovelace"

Adjustment::query()->where('adjustable_label', 'like', "%Lovelace%")->get();

// config/adjustfly.php
'label_attributes' => ['name', 'title', 'label', 'reference', 'email'],

class Setting extends Model
{
    use HasAdjustments;

    public function adjustmentLabel(): ?string
    {
        return "{$this->module} - {$this->key}";
    }
}

Student::withoutAdjustments(function () {
    Student::query()->update(['term' => '2026/1']);
});

use Illuminate\Foundation\Auth\User as Authenticatable;
use Patrixsmart\Adjustfly\Concerns\OwnedAdjustments;

class User extends Authenticatable
{
    use OwnedAdjustments;
}

$user->ownedAdjustments;                 // everything this user changed
$user->paginatedOwnedAdjustments(25);

use Patrixsmart\Adjustfly\Models\Adjustment;

$student->adjustments;                       // newest first
$student->latestAdjustment;                  // single most recent
$student->paginatedAdjustments(25);
$student->simplePaginatedAdjustments(25);

Adjustment::query()->forAdjustable($student)->get();
Adjustment::query()->forType(Student::class)->get();
Adjustment::query()->forUser($user)->get();

$adjustment->changedAttributes();            // ['name', 'email']
$adjustment->before;                         // cast to array
$adjustment->user;                           // the acting user
$adjustment->adjustable;                     // the adjusted model

use Patrixsmart\Adjustfly\Events\AdjustmentRecorded;

Event::listen(function (AdjustmentRecorded $event) {
    // $event->adjustment, $event->adjustable
});

// bootstrap/app.php
->withSchedule(fn ($schedule) => $schedule->command('adjustfly:prune')->daily())

use Patrixsmart\Adjustfly\Models\Adjustment;

class AdjustmentPolicy
{
    public function viewAny(User $user): bool
    {
        return $user->isAdmin();
    }

    public function view(User $user, Adjustment $adjustment): bool
    {
        return $user->isAdmin();
    }
}

// AppServiceProvider::boot()
Gate::policy(Adjustment::class, AdjustmentPolicy::class);

// config/adjustfly.php
'routes' => [
    'enabled' => true,
    'prefix' => 'api',
    'middleware' => ['api', 'auth:sanctum'],
],

Schema::table('adjustments', function (Blueprint $table) {
    $table->string('adjustable_label')->nullable()->after('adjustable_id');
});
sh
php artisan vendor:publish --tag="adjustfly-config"
php artisan vendor:publish --tag="adjustfly-migrations"
php artisan migrate
sh
php artisan adjustfly:prune

php artisan adjustfly:prune --pretend      # report, delete nothing
php artisan adjustfly:prune --days=90      # override the configured window
php artisan adjustfly:prune --chunk=500    # smaller batches on big tables

GET /api/adjustments?adjustable_type=...&adjustable_id=5&event=updated&per_page=25
GET /api/adjustments/{adjustment}