Skip to content

[5.8] Replicating model event#28077

Merged
taylorotwell merged 1 commit into
laravel:5.8from
jerguslejko:replicating-event
Apr 4, 2019
Merged

[5.8] Replicating model event#28077
taylorotwell merged 1 commit into
laravel:5.8from
jerguslejko:replicating-event

Conversation

@jerguslejko

Copy link
Copy Markdown
Contributor

This PR introduces "replicating" model event which is fired when $model->replicate() is called.

This is useful when "replication" is a part of the domain logic. My usecase: I need to reset an attribute every time a model is replicated, because that attribute is specific to the model.

@taylorotwell

Copy link
Copy Markdown
Member

How many different models do you have this use case for? Is it mainly a single model?

@AbdelElrafa

Copy link
Copy Markdown
Contributor

@jerguslejko Why not have a replicating and a replicated event?

@devcircus

Copy link
Copy Markdown
Contributor

Maybe do the replication in a method on your model and fire a custom event. IMO it doesn't seem like a common enough use-case for core.

@taylorotwell

Copy link
Copy Markdown
Member

Maybe do the replication in a method on your model and fire a custom event. IMO it doesn't seem like a common enough use-case for core.

This is kinda what I was getting at with my comment. If you're just doing this on a particular model it may be easier to just make it a method in your application.

@jerguslejko

Copy link
Copy Markdown
Contributor Author

@AbdelElrafa replicating a model does not create a new database record. therefore replicated would be the same as replicating

@taylorotwell I need to be able to dynamically register replication logic in a service provider. using the replicating model event makes it super easy

@AbdelElrafa

AbdelElrafa commented Apr 2, 2019

Copy link
Copy Markdown
Contributor

@jerguslejko I was thinking, like during the replicating event you can make some api call to do something, and during the replicated event you can just save the model automatically. Give the user the choice like we do with the other events.

Take a look at this, https://laravel.com/docs/5.0/eloquent#model-events.
Like during the replicating event you can stop it by returning false.

@taylorotwell taylorotwell merged commit 7d24f91 into laravel:5.8 Apr 4, 2019
@jerguslejko jerguslejko deleted the replicating-event branch April 4, 2019 13:31
@ShekhSaifuddin007

This comment has been minimized.

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.

5 participants