From 59d7f94d0fa97c1a42714b16cb418c6ceb9f647c Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Fri, 3 Jul 2026 15:53:34 +0700 Subject: [PATCH] feat: register ActivityBuffer in AppServiceProvider to enhance activity logging capabilities --- app/Providers/AppServiceProvider.php | 6 +++- app/Support/ActivityLog/ActivityBuffer.php | 42 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 app/Support/ActivityLog/ActivityBuffer.php diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 0e38c8a..8027ecf 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Gate; use Illuminate\Support\ServiceProvider; use Illuminate\Validation\Rules\Password; +use Spatie\Activitylog\Support\ActivityBuffer; class AppServiceProvider extends ServiceProvider { @@ -19,7 +20,10 @@ class AppServiceProvider extends ServiceProvider */ public function register(): void { - // + $this->app->scoped( + ActivityBuffer::class, + \App\Support\ActivityLog\ActivityBuffer::class + ); } /** diff --git a/app/Support/ActivityLog/ActivityBuffer.php b/app/Support/ActivityLog/ActivityBuffer.php new file mode 100644 index 0000000..806513d --- /dev/null +++ b/app/Support/ActivityLog/ActivityBuffer.php @@ -0,0 +1,42 @@ +pending)) { + return; + } + + $records = $this->pending; + + // Collect all unique keys from all records + $allKeys = []; + foreach ($records as $record) { + foreach ($record as $key => $val) { + $allKeys[$key] = true; + } + } + + // Normalize records so that every record has the exact same keys in the same order + $normalizedRecords = []; + foreach ($records as $record) { + $normalized = []; + foreach ($allKeys as $key => $_) { + $normalized[$key] = array_key_exists($key, $record) ? $record[$key] : null; + } + $normalizedRecords[] = $normalized; + } + + $modelClass = Config::activityModel(); + + $modelClass::query()->insert($normalizedRecords); + + $this->pending = []; + } +}