Skip to content

Dispatch event after performing a sort#178

Merged
freekmurze merged 5 commits intospatie:mainfrom
chrispage1:feature/dispatch-event
Jun 4, 2024
Merged

Dispatch event after performing a sort#178
freekmurze merged 5 commits intospatie:mainfrom
chrispage1:feature/dispatch-event

Conversation

@chrispage1
Copy link
Copy Markdown
Contributor

@chrispage1 chrispage1 commented May 31, 2024

When sorting is performed, there is currently no way to clear the cache (just one use case).

This contribution fixes this by dispatching an event.

The event dispatch allows us to listen for an event and perform whatever we need to do after the sort.

I have also added a convenience method called isFor which allows you to check directly against another string or model instance.

An example usage of this would be:

use Spatie\EloquentSortable\EloquentModelSortedEvent as SortEvent;

class SortingListener
{

    public function handle(SortEvent $event): void {
        if ($event->isFor(MyClass::class)) {
            // ToDo: flush our cache
        }
    }
}

I've also merged in the dependabot PR #176 to save you a job!

Thanks!

dependabot Bot and others added 4 commits April 29, 2024 17:56
Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 1.6.0 to 2.1.0.
- [Release notes](https://github.com/dependabot/fetch-metadata/releases)
- [Commits](dependabot/fetch-metadata@v1.6.0...v2.1.0)

---
updated-dependencies:
- dependency-name: dependabot/fetch-metadata
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Added convenience helper to event
…ctions/dependabot/fetch-metadata-2.1.0' into feature/dispatch-event
@freekmurze
Copy link
Copy Markdown
Member

Nice, could you also update the readme with an example on how to use this?

@chrispage1
Copy link
Copy Markdown
Contributor Author

Nice, could you also update the readme with an example on how to use this?

Of course, I have updated the README file 👍

Chris.

@freekmurze freekmurze merged commit a8d09f5 into spatie:main Jun 4, 2024
@freekmurze
Copy link
Copy Markdown
Member

Thanks!

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.

2 participants