Skip to content

Add initially and finally lifecycle hooks to Filters + Update Stub Generation#33

Merged
kettasoft merged 4 commits intomasterfrom
feat/filter-lifecycle-hooks
Nov 12, 2025
Merged

Add initially and finally lifecycle hooks to Filters + Update Stub Generation#33
kettasoft merged 4 commits intomasterfrom
feat/filter-lifecycle-hooks

Conversation

@kettasoft
Copy link
Owner

This PR introduces two protected methods within the Filterable class:

  • initially(): Executes before any filters are applied, allowing pre-filter modifications to the query builder.
  • finally(): Executes after all filters are applied, allowing post-filter adjustments or cleanup.

These hooks provide users with simple lifecycle entry points without the need for implementing an interface.

🧱 Stub Update

When generating a new filter via the CLI command, the stub now includes the initially and finally methods by default:

public function initially(Builder $query): Builder
{
    return $query;
}

public function finally(Builder $query): Builder
{
    return $query;
}

This ensures developers are aware of the available lifecycle hooks and can easily customize pre/post filtering behavior.

💡 Example

class UserFilter extends Filterable
{
    public function initially(Builder $query): Builder
    {
        return $query->whereNotNull('users.id');
    }

    public function finally(Builder $query): Builder
    {
        return $query->orderByDesc('created_at');
    }
}

Changes

  • Added initially and finally lifecycle hooks to Filterable class.
  • Updated filter stub file to include initially and finally methods.
  • Added documentation references and tests for the new lifecycle behavior.

🧪 Testing

  • Added unit tests to verify:

    • initially is executed before filters are applied.
    • finally is executed after all filters are applied.
    • Query builder instance is passed and returned correctly.

The initially method runs before applying filters, and finally runs right after.

@kettasoft kettasoft merged commit 3c7d022 into master Nov 12, 2025
1 check passed
@kettasoft kettasoft deleted the feat/filter-lifecycle-hooks branch November 18, 2025 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant