Skip to content

Commit

Permalink
Merge pull request #14 from phi-rakib/bugfix/model-observers-deleting…
Browse files Browse the repository at this point in the history
…-is-not-working

Bugfix/model observers deleting is not working
  • Loading branch information
phi-rakib authored Jun 2, 2024
2 parents 1e67db2 + 08f37b5 commit 37f95a9
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 10 deletions.
3 changes: 3 additions & 0 deletions app/Http/Controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function destroy(Account $account)
{
Gate::authorize('delete', $account);

$account->deleted_by = auth()->id();
$account->save();

$account->delete(); // soft delete

return response()->json(['message' => 'Account deleted successfully'], 204);
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/DepositController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public function destroy(Deposit $deposit)
{
Gate::authorize('delete', $deposit);

$deposit->deleted_by = auth()->id();
$deposit->save();

$deposit->delete();

return response()->json(['message' => 'Deposited amount deleted'], 204);
Expand Down
5 changes: 0 additions & 5 deletions app/Observers/AccountObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,4 @@ public function updating(Account $account)
{
$account->updated_by = auth()->id();
}

public function deleting(Account $account): void
{
$account->deleted_by = auth()->id();
}
}
5 changes: 0 additions & 5 deletions app/Observers/DepositObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,4 @@ public function updating(Deposit $deposit)
{
$deposit->updated_by = auth()->id();
}

public function deleting(Deposit $deposit)
{
$deposit->deleted_by = auth()->id();
}
}
1 change: 1 addition & 0 deletions tests/Feature/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function test_user_can_delete_an_account()

$this->assertSoftDeleted('accounts', [
'id' => $account->id,
'deleted_by' => $this->user->id,
]);
}

Expand Down
1 change: 1 addition & 0 deletions tests/Feature/DepositTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function test_user_can_delete_deposit()

$this->assertSoftDeleted('deposits', [
'id' => $deposit->id,
'deleted_by' => $this->user->id,
]);
}

Expand Down

0 comments on commit 37f95a9

Please sign in to comment.