-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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.5] Add 'retrieved' model event #20852
Conversation
Thanks for the PR. Good work. 👍 My only concern is, are other people already using the |
Great. Missed that. |
@GrahamCampbell Correct, but this would be the case whatever the name of the event is. Do you see any way to go around this potential issue? If not, this change could be added to the 5.5 upgrade guide (I guess now is the best time to do so). @tillkruss Do you think I should also update the 5.5 changelog? |
You don't have to, I'll already do it anyhow. |
Loving this PR! A while back I had a great usecase for this, I will definately go back and refactor if this is merged in. |
Would be worth having a documentation PR for this. |
@taylorotwell Here is the PR for the docs: laravel/docs#3606 Thanks for the merge :) |
Nice to have this handled, looking forward to using it! |
Aren't we firing this in the wrong place? Eager loaded relationships are not available in protected static function boot()
{
parent::boot();
static::retrieved(function (SomeModel $m) {
dd($m); // no relationships were hydrated at this point
});
}
...
SomeModel::with('whateverRelation')->get(); ping @miclf |
@Kyslik This event is about when the model itself is retrieved, independently of any relationship it might have, so the current implementation makes sense to me. I coded this almost a year ago, so I obviously can’t retrieve all of the details off the top of my head, but I think that firing this event as soon as technically possible gives more possibilities to the developer to potentially alter the following operations that are done with the model (including, but not limited to, how and which relationships are (eager-)loaded. There might also be constraints preventing to call this event later in the retrieval process (resulting in it to be called ‘too late’), but I’m just speculating here. If you feel and think this event should be triggered after the retrieval of relationships, do feel free to submit a change. I cannot guarantee I would have time to help or look into it, but I’ll certainly do it if I can :) Another event would also be a possible implementation (something like |
@miclf thank you for taking time to respond so fast. I actually did some magic with relations in To me There were talks about some more eventing in laravel/ideas#1197 I took a look at the code and I don't think it is possible to fire event in model itself after relationships are eager-loaded. I bet I do not understand it as much... |
As discussed with @taylorotwell at Laracon EU, this simple PR allows Eloquent models to fire a
retrieved
event as soon as their contents are retrieved from the database.Use case
This comes from the following use case:
The second case is pretty easy: I just need to register an event on my model, for example in its
boot()
method:However, there is no symmetric way to execute code when an existing model is retrieved from the database.
The currently most straightforward way to implement this behavior is to extend the
newFromBuilder()
method on our custom model, like so:This works, but it’s clearly not as elegant nor as practical as just registering a listener on the model.
What this PR makes possible
This pull request would allow to satisfy the use case, for example, in the following way:
Of course, you could also use an observer class instead, or any other working way you could think of.
Notes
retrieving
event, because I cannot find any use case for it. More importantly, I don’t really see any place where aretrieving
event would make sense. Just before hydrating the model? Before running the SQL query? I don’t have any satisfying answer;*ed
model events have tests (only the*ing
events require tests), I didn’t add any;