Skip to content

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