Skip to main content

Overview

The CancelResourceRequestAction class handles the cancellation of pending resource requests. It logs the cancellation and permanently deletes the pending request from the database.
This action does not have any constructor dependencies. It can be instantiated directly without dependency injection.

Method Signature

public function execute(
    PendingResource $pending,
    User $user
): void

Parameters

pending
PendingResource
required
The pending resource request to cancel (Eloquent model instance)
user
User
required
The user cancelling the request (used for audit logging)

Return Value

This action returns void (no return value).

Side Effects

This action performs the following side effects:
  1. Audit Log: Creates a ResourceLog entry with action type Cancelled before deletion
  2. Database Delete: Permanently deletes the PendingResource record from the database
Unlike DenyResourceAction, this action permanently deletes the pending request. The request cannot be recovered after cancellation, though the action is recorded in the audit log.

Example Usage

use App\Actions\CancelResourceRequestAction;
use App\Models\{User, PendingResource};

// Instantiate the action (no dependencies required)
$action = new CancelResourceRequestAction();

// Fetch the pending request
$pending = PendingResource::find(1);
$user = User::find(2);

// Execute the action
$action->execute(
    pending: $pending,
    user: $user
);

// The pending request has been deleted
// PendingResource::find(1) will now return null

Cancel vs Deny

Understand the difference between cancelling and denying requests:
use App\Actions\CancelResourceRequestAction;

// Cancellation permanently deletes the request
$action = new CancelResourceRequestAction();
$action->execute($pending, $user);

// Request is gone from database
// Only audit log remains

Use Cases

Common scenarios for cancellation:
  • User cancels their own request before admin review
  • Admin removes spam or invalid requests from the queue
  • Duplicate requests that need to be cleaned up
  • Outdated requests that are no longer needed
For declined legitimate requests, use DenyResourceAction instead to preserve the full request history.

Audit Trail

Even though the pending request is deleted, the cancellation is recorded in the audit log:
  • Action type: Cancelled
  • Performer: The user who cancelled the request
  • Resource details: Name, address, and other attributes from the pending request
  • Timestamp: When the cancellation occurred
The audit log uses PendingResource::toLogChanges() to capture all relevant request data before deletion, ensuring complete traceability.

Error Scenarios

This action has minimal error scenarios:
  • Database errors: Will throw an exception if the log creation or deletion fails
  • Already deleted: Will throw a model not found exception if the pending resource doesn’t exist

Source Reference

Implementation: /home/daytona/workspace/source/app/Actions/CancelResourceRequestAction.php:17

Build docs developers (and LLMs) love