Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Register many observers for a model #23507

Merged
merged 4 commits into from
Mar 13, 2018
Merged

[5.6] Register many observers for a model #23507

merged 4 commits into from
Mar 13, 2018

Conversation

Alymosul
Copy link
Contributor

@Alymosul Alymosul commented Mar 12, 2018

This PR adds the ability to pass an array of observers to the observe() method for a model instead of registering each of them one by one.
Consider we have a Thread model and we will register the following observers for it: DeleteThreadRepliesObserver, GenerateThreadSlugObserver, ReputationObserver, ThreadObserver

Currently:
\App\Thread::observe(\App\Observers\DeleteThreadRepliesObserver::class);
\App\Thread::observe(\App\Observers\GenerateThreadSlugObserver::class);
\App\Thread::observe(\App\Observers\ReputationObserver::class);
\App\Thread::observe(\App\Observers\ThreadObserver::class);
Proposed:
\App\Thread::observe([
\App\Observers\DeleteThreadRepliesObserver::class,
\App\Observers\GenerateThreadSlugObserver::class,
\App\Observers\ReputationObserver::class,
\App\Observers\ThreadObserver::class
]);

@ntzm
Copy link
Contributor

ntzm commented Mar 13, 2018

This is nice, I've definitely needed this before! However, could you possibly add tests? Thanks

@devcircus
Copy link
Contributor

I second this. Though I personally no longer use observers in most cases. It makes sense. Tests would be nice.

@ludo237
Copy link

ludo237 commented Mar 13, 2018

@devcircus is there a better alternative to observers?

@Alymosul I think you should add tests in order to verify that someone can actually use the old version and the new one

@taylorotwell taylorotwell merged commit 4f8bc0e into laravel:5.6 Mar 13, 2018
@afraca
Copy link
Contributor

afraca commented Mar 14, 2018

Nice addition.
Nitpick: I know this is already merged, but perhaps we could make the registerObserver method protected to be in line with the rest of the model and be easier to override?

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.

6 participants