抱歉,我是 Laravel 的新手,对创建和创建模型事件的区别有点困惑。我在 laravel docs 中找到:
Eloquent models fire several events, allowing you to hook into various points in the model's lifecycle using the following methods: creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored...Whenever a new model is saved for the first time, the creating and created events will fire.
但是创建和创建事件有什么区别?创建和创建的事件总是一起触发吗?或者是否存在创建被触发但未创建的情况?
最佳答案
主要区别(至少对我而言)是:
creating
事件更“强大”,因为作为文档的示例,您可以在 creating
期间取消模型的创建。事件,例如,如果它无效。 created
当模型已经保存到数据库中时将触发事件,因此您无法取消任何内容,您只能准备其他数据,例如,一旦模型被保存。 触发一个事件而不触发另一个事件的可能情况:在文档示例中,如果模型无效,则
creating
事件将被触发,但不会触发 created
事件,因为它无效,并且不会保存到数据库中。你好!
关于Laravel 5 - 创建和创建模型事件有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34540322/