Laravel 5 - 创建和创建模型事件有什么区别?

标签 laravel

抱歉,我是 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/

    相关文章:

    php - routes.php 中的 FatalErrorException 第 113 行 : Call to a member function delete() on null in Laravel 5. 2

    Laravel 如何从子域 URL 中删除 "api"前缀

    javascript - 从编辑表单中的下拉列表中显示国家/地区 Laravel 5.2

    php - passport oauth/authorize 始终重定向登录已修复,但客户端身份验证失败

    arrays - Laravel - Eloquent - 从返回数组中获取特定值

    laravel - find_in_set 与 laravel 中的 join

    php - Laravel Eloquent : keeping all interactions with a model in sync in two different databases

    php - 根据要求进行 Laravel 哈希检查

    laravel - "Could not find driver"执行查询时的消息

    php - 本地主机和 "stream_socket_enable_crypto(): SSL operation failed with code 1"