diff --git a/README.md b/README.md index dd13d2c..a194faf 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,18 @@ composer require amdad121/record-activity-laravel ## Usage +Open your migration file and add this table column. both are optional. + +```php +Schema::create('users', function (Blueprint $table) { + // ... + $table->withCreatedByAndUpdatedBy(); + $table->withDeletedBy(); +}); +``` + +Now this traits add on your model. + ```php isDirty('created_by') && auth()->check()) { + $model->created_by = auth()->user()->id; + } + + if (! $model->isDirty('updated_by') && auth()->check()) { + $model->updated_by = auth()->user()->id; + } + }); + + // when model is updating + static::updating(function ($model) { + if (! $model->isDirty('updated_by') && auth()->check()) { + $model->updated_by = auth()->user()->id; + } + }); + } +} diff --git a/src/DeletedBy.php b/src/DeletedBy.php new file mode 100755 index 0000000..b3c4057 --- /dev/null +++ b/src/DeletedBy.php @@ -0,0 +1,18 @@ +isDirty('deleted_by')) { + $model->deleted_by = auth()->user()->id; + } + }); + } +} diff --git a/src/RecordActivity.php b/src/RecordActivity.php deleted file mode 100755 index 92abb73..0000000 --- a/src/RecordActivity.php +++ /dev/null @@ -1,34 +0,0 @@ -isDirty('created_by')) { - $model->created_by = auth()->user()->id; - } - - if (! $model->isDirty('updated_by')) { - $model->updated_by = auth()->user()->id; - } - }); - - static::updating(function ($model) { - if (! $model->isDirty('updated_by')) { - $model->updated_by = auth()->user()->id; - } - }); - - static::deleting(function ($model) { - if (! $model->isDirty('deleted_by')) { - $model->deleted_by = auth()->user()->id; - } - }); - } -} diff --git a/src/RecordActivityServiceProvider.php b/src/RecordActivityServiceProvider.php index 6ca22db..0dbf720 100644 --- a/src/RecordActivityServiceProvider.php +++ b/src/RecordActivityServiceProvider.php @@ -4,6 +4,7 @@ namespace AmdadulHaq\RecordActivity; +use Illuminate\Database\Schema\Blueprint; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; @@ -19,4 +20,28 @@ public function configurePackage(Package $package): void $package ->name('record-activity-laravel'); } + + public function packageRegistered(): void + { + // Extend the Blueprint class to add custom methods + Blueprint::macro('withCreatedByAndUpdatedBy', function () { + $this->foreignId('created_by')->nullable()->constrained('users')->cascadeOnDelete(); + $this->foreignId('updated_by')->nullable()->constrained('users')->cascadeOnDelete(); + }); + + Blueprint::macro('withDeletedBy', function () { + $this->foreignId('deleted_by')->nullable()->constrained('users')->cascadeOnDelete(); + }); + + // Extend the Blueprint class to add custom drop methods + Blueprint::macro('dropCreatedByAndUpdatedBy', function () { + $this->dropForeign(['created_by', 'updated_by']); + $this->dropColumn(['created_by', 'updated_by']); + }); + + Blueprint::macro('dropDeletedBy', function () { + $this->dropForeign(['deleted_by']); + $this->dropColumn(['deleted_by']); + }); + } }