feat: log activity

This commit is contained in:
myqnrrhmn 2025-07-16 15:14:04 +07:00
parent 097e1e7fcb
commit b5585d76c9
11 changed files with 424 additions and 4 deletions

View File

@ -3,9 +3,12 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class Announcement extends Model
{
use LogsActivity;
protected $table = 'announcements';
protected $fillable = [
@ -27,4 +30,30 @@ public function users()
->withPivot(['status', 'category'])
->withTimestamps();
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['headline', 'content', 'link', 'attachment', 'enhancer_id']) // kolom yang dicatat
->useLogName('announcement') // nama log
->logOnlyDirty(); // hanya log kalau ada perubahan
}
public function getDescriptionForEvent(string $eventName): string
{
switch($eventName){
case 'created':
$eventName = 'dibuat';
break;
case 'updated':
$eventName = 'diubah';
break;
case 'deleted':
$eventName = 'dihapus';
break;
}
return "Pengumuman pengguna '{$this->headline}' telah {$eventName}";
}
}

View File

@ -4,10 +4,12 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class Classification extends Model
{
use SoftDeletes;
use SoftDeletes, LogsActivity;
protected $table = 'classifications';
protected $fillable = [
@ -28,4 +30,30 @@ public function issues(){
public function enhancer(){
return $this->belongsTo(User::class, 'enhancer_id', 'id');
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['name', 'status', 'description', 'enhancer_id']) // kolom yang dicatat
->useLogName('classification') // nama log
->logOnlyDirty(); // hanya log kalau ada perubahan
}
public function getDescriptionForEvent(string $eventName): string
{
switch($eventName){
case 'created':
$eventName = 'dibuat';
break;
case 'updated':
$eventName = 'diubah';
break;
case 'deleted':
$eventName = 'dihapus';
break;
}
return "Klasifikasi '{$this->name}' telah {$eventName}";
}
}

View File

@ -3,9 +3,12 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class Location extends Model
{
use LogsActivity;
protected $table = 'locations';
protected $fillable = [
@ -26,4 +29,29 @@ public function issues(){
public function enhancer(){
return $this->belongsTo(User::class, 'enhancer_id', 'id');
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly([ 'name', 'status', 'description', 'enhancer_id']) // kolom yang dicatat
->useLogName('location') // nama log
->logOnlyDirty(); // hanya log kalau ada perubahan
}
public function getDescriptionForEvent(string $eventName): string
{
switch($eventName){
case 'created':
$eventName = 'dibuat';
break;
case 'updated':
$eventName = 'diubah';
break;
case 'deleted':
$eventName = 'dihapus';
break;
}
return "Lokus '{$this->name}' telah {$eventName}";
}
}

View File

@ -4,10 +4,12 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class News extends Model
{
use SoftDeletes;
use SoftDeletes, LogsActivity;
protected $table = 'news';
protected $fillable = [
@ -25,4 +27,34 @@ class News extends Model
public function author(){
return $this->belongsTo(User::class, 'author_id', 'id');
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['title', 'slug', 'content', 'image', 'link', 'category', 'author_id']) // kolom yang dicatat
->useLogName('news') // nama log
->logOnlyDirty(); // hanya log kalau ada perubahan
}
public function getDescriptionForEvent(string $eventName): string
{
switch($eventName){
case 'created':
$eventName = 'dibuat';
break;
case 'updated':
$eventName = 'diubah';
break;
case 'deleted':
$eventName = 'dihapus';
break;
}
if($this->category == '0'){
return "Berita '{$this->title}' telah {$eventName}";
}else{
return "Pengumuman '{$this->title}' telah {$eventName}";
}
}
}

View File

@ -4,10 +4,12 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class SubClassification extends Model
{
use SoftDeletes;
use SoftDeletes, LogsActivity;
protected $table = 'sub_classifications';
@ -30,4 +32,29 @@ public function issues(){
public function enhancer(){
return $this->belongsTo(User::class, 'enhancer_id', 'id');
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly([ 'name', 'status', 'description', 'classification_id', 'enhancer_id']) // kolom yang dicatat
->useLogName('sub_classification') // nama log
->logOnlyDirty(); // hanya log kalau ada perubahan
}
public function getDescriptionForEvent(string $eventName): string
{
switch($eventName){
case 'created':
$eventName = 'dibuat';
break;
case 'updated':
$eventName = 'diubah';
break;
case 'deleted':
$eventName = 'dihapus';
break;
}
return "Sub Klasifikasi '{$this->name}' telah {$eventName}";
}
}

View File

@ -16,6 +16,7 @@
"laravel/tinker": "^2.9",
"league/flysystem-aws-s3-v3": "^3.0",
"maatwebsite/excel": "^3.1",
"spatie/laravel-activitylog": "^4.10",
"spatie/laravel-settings": "^3.4",
"sqids/sqids": "^0.5.0",
"vinkla/hashids": "^12.0",

154
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b7c265f1ef13691fce1a70859340846c",
"content-hash": "c4a90fa7aa865cc7cd329f8e72f44464",
"packages": [
{
"name": "aws/aws-crt-php",
@ -4929,6 +4929,158 @@
},
"time": "2025-03-23T17:59:05+00:00"
},
{
"name": "spatie/laravel-activitylog",
"version": "4.10.2",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-activitylog.git",
"reference": "bb879775d487438ed9a99e64f09086b608990c10"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-activitylog/zipball/bb879775d487438ed9a99e64f09086b608990c10",
"reference": "bb879775d487438ed9a99e64f09086b608990c10",
"shasum": ""
},
"require": {
"illuminate/config": "^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
"illuminate/database": "^8.69 || ^9.27 || ^10.0 || ^11.0 || ^12.0",
"illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
"php": "^8.1",
"spatie/laravel-package-tools": "^1.6.3"
},
"require-dev": {
"ext-json": "*",
"orchestra/testbench": "^6.23 || ^7.0 || ^8.0 || ^9.0 || ^10.0",
"pestphp/pest": "^1.20 || ^2.0 || ^3.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Spatie\\Activitylog\\ActivitylogServiceProvider"
]
}
},
"autoload": {
"files": [
"src/helpers.php"
],
"psr-4": {
"Spatie\\Activitylog\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
},
{
"name": "Sebastian De Deyne",
"email": "sebastian@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
},
{
"name": "Tom Witkowski",
"email": "dev.gummibeer@gmail.com",
"homepage": "https://gummibeer.de",
"role": "Developer"
}
],
"description": "A very simple activity logger to monitor the users of your website or application",
"homepage": "https://github.com/spatie/activitylog",
"keywords": [
"activity",
"laravel",
"log",
"spatie",
"user"
],
"support": {
"issues": "https://github.com/spatie/laravel-activitylog/issues",
"source": "https://github.com/spatie/laravel-activitylog/tree/4.10.2"
},
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
},
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2025-06-15T06:59:49+00:00"
},
{
"name": "spatie/laravel-package-tools",
"version": "1.92.6",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-package-tools.git",
"reference": "afa90e37741a953d33728e7106a1f24a13fdd808"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-package-tools/zipball/afa90e37741a953d33728e7106a1f24a13fdd808",
"reference": "afa90e37741a953d33728e7106a1f24a13fdd808",
"shasum": ""
},
"require": {
"illuminate/contracts": "^9.28|^10.0|^11.0|^12.0",
"php": "^8.0"
},
"require-dev": {
"mockery/mockery": "^1.5",
"orchestra/testbench": "^7.7|^8.0|^9.0|^10.0",
"pestphp/pest": "^1.23|^2.1|^3.1",
"phpunit/php-code-coverage": "^9.0|^10.0|^11.0",
"phpunit/phpunit": "^9.5.24|^10.5|^11.5",
"spatie/pest-plugin-test-time": "^1.1|^2.2"
},
"type": "library",
"autoload": {
"psr-4": {
"Spatie\\LaravelPackageTools\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"role": "Developer"
}
],
"description": "Tools for creating Laravel packages",
"homepage": "https://github.com/spatie/laravel-package-tools",
"keywords": [
"laravel-package-tools",
"spatie"
],
"support": {
"issues": "https://github.com/spatie/laravel-package-tools/issues",
"source": "https://github.com/spatie/laravel-package-tools/tree/1.92.6"
},
"funding": [
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2025-07-14T08:02:47+00:00"
},
{
"name": "spatie/laravel-settings",
"version": "3.4.2",

52
config/activitylog.php Normal file
View File

@ -0,0 +1,52 @@
<?php
return [
/*
* If set to false, no activities will be saved to the database.
*/
'enabled' => env('ACTIVITY_LOGGER_ENABLED', true),
/*
* When the clean-command is executed, all recording activities older than
* the number of days specified here will be deleted.
*/
'delete_records_older_than_days' => 365,
/*
* If no log name is passed to the activity() helper
* we use this default log name.
*/
'default_log_name' => 'default',
/*
* You can specify an auth driver here that gets user models.
* If this is null we'll use the current Laravel auth driver.
*/
'default_auth_driver' => null,
/*
* If set to true, the subject returns soft deleted models.
*/
'subject_returns_soft_deleted_models' => false,
/*
* This model will be used to log activity.
* It should implement the Spatie\Activitylog\Contracts\Activity interface
* and extend Illuminate\Database\Eloquent\Model.
*/
'activity_model' => \Spatie\Activitylog\Models\Activity::class,
/*
* This is the name of the table that will be created by the migration and
* used by the Activity model shipped with this package.
*/
'table_name' => env('ACTIVITY_LOGGER_TABLE_NAME', 'activity_log'),
/*
* This is the database connection that will be used by the migration and
* the Activity model shipped with this package. In case it's not set
* Laravel's database.default will be used instead.
*/
'database_connection' => env('ACTIVITY_LOGGER_DB_CONNECTION'),
];

View File

@ -0,0 +1,27 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->create(config('activitylog.table_name'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('log_name')->nullable();
$table->text('description');
$table->nullableMorphs('subject', 'subject');
$table->nullableMorphs('causer', 'causer');
$table->json('properties')->nullable();
$table->timestamps();
$table->index('log_name');
});
}
public function down()
{
Schema::connection(config('activitylog.database_connection'))->dropIfExists(config('activitylog.table_name'));
}
}

View File

@ -0,0 +1,22 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddEventColumnToActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->string('event')->nullable()->after('subject_type');
});
}
public function down()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->dropColumn('event');
});
}
}

View File

@ -0,0 +1,22 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddBatchUuidColumnToActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->uuid('batch_uuid')->nullable()->after('properties');
});
}
public function down()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->dropColumn('batch_uuid');
});
}
}