Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
amdad121 committed Dec 31, 2023
1 parent f32f031 commit 3b3948b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 36 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

Expand All @@ -25,12 +37,18 @@ namespace App\Models;
use AmdadulHaq\RecordActivity\RecordActivity;
// ...

class User extends Authenticatable
class User extends Model
{
use RecordActivity;
use CreatedByAndUpdatedBy, DeletedBy;
}
```

Now run the migrations with this command.

```bash
php artisan migrate
```

## Testing

```bash
Expand Down
29 changes: 29 additions & 0 deletions src/CreatedByAndUpdatedBy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace AmdadulHaq\RecordActivity;

trait CreatedByAndUpdatedBy
{
public static function bootCreatedByAndUpdatedBy(): void
{
// when model is created
static::creating(function ($model) {
if (! $model->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;
}
});
}
}
18 changes: 18 additions & 0 deletions src/DeletedBy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace AmdadulHaq\RecordActivity;

trait DeletedBy
{
public static function bootDeletedBy(): void
{
// when model is deleting
static::deleting(function ($model) {
if (! $model->isDirty('deleted_by')) {
$model->deleted_by = auth()->user()->id;
}
});
}
}
34 changes: 0 additions & 34 deletions src/RecordActivity.php

This file was deleted.

25 changes: 25 additions & 0 deletions src/RecordActivityServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AmdadulHaq\RecordActivity;

use Illuminate\Database\Schema\Blueprint;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -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();

Check failure on line 28 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::foreignId().

Check failure on line 28 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::foreignId().
$this->foreignId('updated_by')->nullable()->constrained('users')->cascadeOnDelete();

Check failure on line 29 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::foreignId().

Check failure on line 29 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::foreignId().
});

Blueprint::macro('withDeletedBy', function () {
$this->foreignId('deleted_by')->nullable()->constrained('users')->cascadeOnDelete();

Check failure on line 33 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::foreignId().

Check failure on line 33 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::foreignId().
});

// Extend the Blueprint class to add custom drop methods
Blueprint::macro('dropCreatedByAndUpdatedBy', function () {
$this->dropForeign(['created_by', 'updated_by']);

Check failure on line 38 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::dropForeign().

Check failure on line 38 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::dropForeign().
$this->dropColumn(['created_by', 'updated_by']);

Check failure on line 39 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::dropColumn().

Check failure on line 39 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::dropColumn().
});

Blueprint::macro('dropDeletedBy', function () {
$this->dropForeign(['deleted_by']);

Check failure on line 43 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::dropForeign().

Check failure on line 43 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::dropForeign().
$this->dropColumn(['deleted_by']);

Check failure on line 44 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::dropColumn().

Check failure on line 44 in src/RecordActivityServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method AmdadulHaq\RecordActivity\RecordActivityServiceProvider::dropColumn().
});
}
}

0 comments on commit 3b3948b

Please sign in to comment.