惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
博客园 - 司徒正美
P
Privacy International News Feed
G
Google Developers Blog
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
K
Kaspersky official blog
I
InfoQ
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
量子位
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
S
Security Affairs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
S
Security Archives - TechRepublic
V
Visual Studio Blog
T
Troy Hunt's Blog
S
Secure Thoughts
F
Fortinet All Blogs
V
V2EX
The Register - Security
The Register - Security
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - 陈达辉

win10安装redis 基于SpringBoot的在线教育系统【源码开源】【建议收藏】 - 陈达辉 eduYouke在线教育点播系统 解决ThinkPHP6 控制器不存在:app\controller\Index - 陈达辉 - 博客园 ubuntu下安装YApi 低版本idea中SpringBoot项目启动失败,提示找不到 javax/servlet/ServletContext类 nginx -s reload 与 service nginx restart 的区别 安装pip install pymysql碰到的问题,升级pip报错:Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-vZoYWX/pip/ python 服务器后台运行 Git冲突:commit your changes or stash them before you can merge. 解决办法 安装node、vue相关问题 初学JAVA-10-java代码的组织结构 IntelliJ IDEA 2020.2 破解版(附永久破解激活方法)Java开发者必备的神器 本地连接虚拟机redis,解决redis connection refused: connect问题 linux如何删除php7 使用vscode开发php nginx: [emerg] open() "/etc/nginx/fastcgi.conf" failed ubuntu php7.2安装php-redis扩展 Ubuntu16.04安装Redis
laravel观察者模式使用及注意事项
陈达辉 · 2021-01-21 · via 博客园 - 陈达辉

一、先讲观察者使用流程

          1.先创建一个 App\Observers 文件夹,

          2.然后创建想要操作的模型对应的 observer,比如说创建一个 AgentLogsObservers

                 

           3.然后到 AppServiceProvider 的 boot 方法当中进行注册,也可以是其他的 ServiceProvider,不固定。

// 为 AgentLogs 模型注册观察者
AgentLogs::observe(AgentLogsObservers::class);

注意:(别忘了引入 model,做完这些我们就可以各种操作了。)

观察者方法有很对比如:

retrieved,            #获取到模型实例后触发
creating,             #创建过程前                                * 常用
created,              #创建成功后                                * 常用
updating,             #更新过程前                                * 常用
updated,              #更新成功后                                * 常用
saving,               #代表这两个方法的集合creating,updating       * 常用
saved,                #代表这两个方法的集合created,updated         * 常用
deleting,             #删除过程前                                * 常用
deleted,              #删除过程后                                * 常用
restoring,            #恢复软删除记录前触发
restored,             #恢复软删除记录后触发

4、接下来我们编写观察者方法 

/**
     * 监听修改事件.
     *
     * @param  \App\Models\Mall\AgentLogs  $agentLogs
     * @return void
     */
    public function saved(AgentLogs $agentLogs){
        ownLogs('observers.log','观察者-saved 收到执行通知 执行完毕!');
    }
    public function updated(AgentLogs $agentLogs){
        ownLogs('observers.log','观察者-updated 收到执行通知 执行完毕!');
    }

5、随便找个控制器去操作数据库,可以观察走到了个方法,以及先后顺序日志打印的顺序。

原理:

  • 当模型已存在,非新建时,事件触发顺序如下:
    saving -> updating -> updated -> saved
  • 当模型不存在,即需要新增时,事件触发顺序如下:
    saving -> creating -> created -> saved

      这里我只测试 用save()方法去修改一条记录,如下图代码:

日志输出结果:

      从下图可以发现:一个save()方法执行成功的话,会影响updated  和 saved,并且是先触发updated事件再触发saved事件,

刚好印证了上面的官方事件触发顺序:saving -> updating -> updated -> saved

所以实际使用观察者的时候二选一即可,千万别两个事件重复交叉了