pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <small id='Ai5Hq'></small><noframes id='Ai5Hq'>

      <bdo id='Ai5Hq'></bdo><ul id='Ai5Hq'></ul>

    <tfoot id='Ai5Hq'></tfoot>

  • <legend id='Ai5Hq'><style id='Ai5Hq'><dir id='Ai5Hq'><q id='Ai5Hq'></q></dir></style></legend>

      <i id='Ai5Hq'><tr id='Ai5Hq'><dt id='Ai5Hq'><q id='Ai5Hq'><span id='Ai5Hq'><b id='Ai5Hq'><form id='Ai5Hq'><ins id='Ai5Hq'></ins><ul id='Ai5Hq'></ul><sub id='Ai5Hq'></sub></form><legend id='Ai5Hq'></legend><bdo id='Ai5Hq'><pre id='Ai5Hq'><center id='Ai5Hq'></center></pre></bdo></b><th id='Ai5Hq'></th></span></q></dt></tr></i><div class="iowceua" id='Ai5Hq'><tfoot id='Ai5Hq'></tfoot><dl id='Ai5Hq'><fieldset id='Ai5Hq'></fieldset></dl></div>

        在 Laravel 5 中使用模型事件監聽器

        Using Model Events Listener in Laravel 5(在 Laravel 5 中使用模型事件監聽器)

        1. <i id='ZxeTI'><tr id='ZxeTI'><dt id='ZxeTI'><q id='ZxeTI'><span id='ZxeTI'><b id='ZxeTI'><form id='ZxeTI'><ins id='ZxeTI'></ins><ul id='ZxeTI'></ul><sub id='ZxeTI'></sub></form><legend id='ZxeTI'></legend><bdo id='ZxeTI'><pre id='ZxeTI'><center id='ZxeTI'></center></pre></bdo></b><th id='ZxeTI'></th></span></q></dt></tr></i><div class="eeuqyew" id='ZxeTI'><tfoot id='ZxeTI'></tfoot><dl id='ZxeTI'><fieldset id='ZxeTI'></fieldset></dl></div>
              <tbody id='ZxeTI'></tbody>
                <bdo id='ZxeTI'></bdo><ul id='ZxeTI'></ul>

                  <small id='ZxeTI'></small><noframes id='ZxeTI'>

                  <legend id='ZxeTI'><style id='ZxeTI'><dir id='ZxeTI'><q id='ZxeTI'></q></dir></style></legend>
                  <tfoot id='ZxeTI'></tfoot>

                  本文介紹了在 Laravel 5 中使用模型事件監聽器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想確保我在 Laravel 5 中正確使用了模型事件偵聽器,并且我沒有搞砸任何事情(偵聽器與處理程序?).我的解決方案工作正常,但我不知道我是否按照 Laravel 5 的概念和約定開發.

                  I would like to make sure that I correctly used model events listeners in Laravel 5 and I didn't messed up nothing (listener vs handler?). My solution works fine, but I wonder if I developed according to concept and convention of Laravel 5.

                  目標:保存模型時,始終將 $issue->status_id 設置為某個值.

                  Goal: Always set $issue->status_id on some value when model is saving.

                  在 appProvidersEventServiceProvider.php 中

                  In appProvidersEventServiceProvider.php

                  <?php namespace AppProviders;
                  
                  ...
                  
                  class EventServiceProvider extends ServiceProvider {
                  
                      ...
                  
                      public function boot(DispatcherContract $events)
                      {
                          parent::boot($events);
                  
                          Issue::saving('AppHandlersEventsSetIssueStatus');
                      }
                  
                  }
                  

                  在 appHandlersEventsSetIssueStatus.php 中

                  In appHandlersEventsSetIssueStatus.php

                  <?php namespace AppHandlersEvents;
                  
                  ...
                  
                  class SetIssueStatus {
                  
                      ...
                  
                      public function handle(Issue $issue)
                      {
                          if (something)
                          {   
                              $issueStatus = IssueStatus::where(somethingElse)->firstOrFail();
                          }
                          else 
                          {
                              $issueStatus = IssueStatus::where(somethingAnother)->firstOrFail();
                          }
                  
                          // issue_status() is One-to-One relations with IssueType (belongsTo)
                          $issue->issue_status()->associate($issueStatus);
                      }
                  
                  }
                  

                  感謝您的寶貴時間.

                  推薦答案

                  正如您所說,您有一個有效的版本并且它是有效的,現在由您來確定它是否適合您.

                  As you said you have a working version and it's a valid one, now that's up to you to figure out if it's ok for you.

                  澄清一下,我并不是說這些是更好的解決方案,它們只是一種有效的不同方式.

                  Just to clarify I'm not saying that these are better solutions, they are just a valid different way.

                  由于您所做的是特定于 Issue 模型的,或者至少它似乎不是一個通用事件,因此您可以直接在您的模型上進行設置

                  Since what you are doing is specific to the Issue model or at least it doesn't seem to be a generic event, you could set it up on your model directly

                  <?php namespace App;
                  
                  use IlluminateDatabaseEloquentModel;
                  use IssueStatus;
                  
                  class Issue extends Model {
                  
                  
                      protected static function boot()
                      {
                          parent::boot();
                  
                          static::saving(function($issue){
                              if (something)
                              {   
                                  $issueStatus = IssueStatus::where(somethingElse)->firstOrFail();
                              }
                              else 
                              {
                                  $issueStatus = IssueStatus::where(somethingAnother)->firstOrFail();
                              }
                  
                              // issue_status() is One-to-One relations with IssueType (belongsTo)
                              $issue->issue_status()->associate($issueStatus);
                  
                          });
                      }
                  }
                  

                  但如果您的事件確實是一個通用事件,并且您想在多個模型中使用它,則可以實現相同的目的.您只需要從模型中提取代碼并使用特征(就像軟刪除一樣)

                  but if your event is indeed a generic one and you want to use it across multiple Models, you could achieve the same thing. You just need to extract the code from the model and use traits (like you do with soft deletes)

                  首先,我們創建我們的 trait(在本例中,我們在應用程序的根目錄上創建)并從模型中提取我之前編寫的代碼:

                  First we create our trait(in this case we created on the root of our App) and extract the code, I wrote before, from the model:

                  <?php namespace App
                  
                  use IssueStatus;
                  
                  trait IssueStatusSetter
                  {
                      protected static function boot()
                      {
                          parent::boot();
                  
                          static::saving(function($model){
                              if (something)
                              {   
                                  $issueStatus = IssueStatus::where(somethingElse)->firstOrFail();
                              }
                              else 
                              {
                                  $issueStatus = IssueStatus::where(somethingAnother)->firstOrFail();
                              }
                  
                              // issue_status() is One-to-One relations with IssueType (belongsTo)
                              $model->issue_status()->associate($issueStatus);
                  
                          });
                      }
                  }
                  

                  現在在你想使用它的模型上,你只需導入特征并聲明它的使用:

                  Now on the models where you want to use it, you just import the trait and declare it's use:

                  <?php namespace App;
                  
                  use IlluminateDatabaseEloquentModel;
                  use IssueStatusSetter;
                  
                  class Issue extends Model {
                  
                      use IssueStatusSetter;
                  
                  }
                  

                  現在我向您展示的最后一個選項是一個通用選項,您可以將其應用于每個模型,只需聲明它在模型頂部使用即可.

                  Now this last option I showed you it's a generic option you have that you could apply to every Model by just declaring it's use on the top of your model.

                  這篇關于在 Laravel 5 中使用模型事件監聽器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)
                  1. <i id='Azfl4'><tr id='Azfl4'><dt id='Azfl4'><q id='Azfl4'><span id='Azfl4'><b id='Azfl4'><form id='Azfl4'><ins id='Azfl4'></ins><ul id='Azfl4'></ul><sub id='Azfl4'></sub></form><legend id='Azfl4'></legend><bdo id='Azfl4'><pre id='Azfl4'><center id='Azfl4'></center></pre></bdo></b><th id='Azfl4'></th></span></q></dt></tr></i><div class="u20suuy" id='Azfl4'><tfoot id='Azfl4'></tfoot><dl id='Azfl4'><fieldset id='Azfl4'></fieldset></dl></div>
                  2. <tfoot id='Azfl4'></tfoot>

                    • <small id='Azfl4'></small><noframes id='Azfl4'>

                      • <bdo id='Azfl4'></bdo><ul id='Azfl4'></ul>

                            <tbody id='Azfl4'></tbody>

                            <legend id='Azfl4'><style id='Azfl4'><dir id='Azfl4'><q id='Azfl4'></q></dir></style></legend>
                            主站蜘蛛池模板: 闸阀_截止阀_止回阀「生产厂家」-上海卡比阀门有限公司 | 工控机-图像采集卡-PoE网卡-人工智能-工业主板-深圳朗锐智科 | 铝箔袋,铝箔袋厂家,东莞铝箔袋,防静电铝箔袋,防静电屏蔽袋,防静电真空袋,真空袋-东莞铭晋让您的产品与众不同 | 空气能暖气片,暖气片厂家,山东暖气片,临沂暖气片-临沂永超暖通设备有限公司 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 上海恒驭仪器有限公司-实验室平板硫化机-小型平板硫化机-全自动平板硫化机 | 耳模扫描仪-定制耳机设计软件-DLP打印机-asiga打印机-fitshape「飞特西普」 | 定量包装秤,吨袋包装称,伸缩溜管,全自动包装秤,码垛机器人,无锡市邦尧机械工程有限公司 | 清水混凝土修复_混凝土色差修复剂_混凝土色差调整剂_清水混凝土色差修复_河南天工 | 一体化净水器_一体化净水设备_一体化水处理设备-江苏旭浩鑫环保科技有限公司 | RTO换向阀_VOC高温阀门_加热炉切断阀_双偏心软密封蝶阀_煤气蝶阀_提升阀-湖北霍科德阀门有限公司 | 制冷采购电子商务平台——制冷大市场| 郑州外墙清洗_郑州玻璃幕墙清洗_郑州开荒保洁-河南三恒清洗服务有限公司 | 干式变压器厂_干式变压器厂家_scb11/scb13/scb10/scb14/scb18干式变压器生产厂家-山东科锐变压器有限公司 | 无水硫酸铝,硫酸铝厂家-淄博双赢新材料科技有限公司 | 耐酸碱胶管_耐腐蚀软管总成_化学品输送软管_漯河利通液压科技耐油耐磨喷砂软管|耐腐蚀化学软管 | 纯水电导率测定仪-万用气体检测仪-低钠测定仪-米沃奇科技(北京)有限公司www.milwaukeeinst.cn 锂辉石检测仪器,水泥成分快速分析仪-湘潭宇科分析仪器有限公司 手术室净化装修-手术室净化工程公司-华锐手术室净化厂家 | 无压烧结银_有压烧结银_导电银胶_导电油墨_导电胶-善仁(浙江)新材料 | 全自动包装机_灌装机生产厂家-迈驰包装设备有限公司 | 振动筛,震动筛,圆形振动筛,振动筛价格,振动筛厂家-新乡巨宝机电 蒸汽热收缩机_蒸汽发生器_塑封机_包膜机_封切收缩机_热收缩包装机_真空机_全自动打包机_捆扎机_封箱机-东莞市中堡智能科技有限公司 | 产业规划_产业园区规划-产业投资选址及规划招商托管一体化服务商-中机院产业园区规划网 | 泰国专线_泰国物流专线_广州到泰国物流公司-泰廊曼国际 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 爆炸冲击传感器-无线遥测传感器-航天星百科 | 郑州水质检测中心_井水检测_河南废气检测_河南中环嘉创检测 | 净气型药品柜-试剂柜-无管道净气型通风柜-苏州毕恩思 | 珠海白蚁防治_珠海灭鼠_珠海杀虫灭鼠_珠海灭蟑螂_珠海酒店消杀_珠海工厂杀虫灭鼠_立净虫控防治服务有限公司 | 飞扬动力官网-广告公司管理软件,广告公司管理系统,喷绘写真条幅制作管理软件,广告公司ERP系统 | 济南轻型钢结构/济南铁艺护栏/济南铁艺大门-济南燕翔铁艺制品有限公司 | 沉降天平_沉降粒度仪_液体比重仪-上海方瑞仪器有限公司 | 蒸汽热收缩机_蒸汽发生器_塑封机_包膜机_封切收缩机_热收缩包装机_真空机_全自动打包机_捆扎机_封箱机-东莞市中堡智能科技有限公司 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 海南在线 海南一家 | 智慧物联网行业一站式解决方案提供商-北京东成基业 | 工业设计,人工智能,体验式3D展示的智能技术交流服务平台-纳金网 J.S.Bach 圣巴赫_高端背景音乐系统_官网 | 滑石粉,滑石粉厂家,超细滑石粉-莱州圣凯滑石有限公司 | 贴片电感_贴片功率电感_贴片绕线电感_深圳市百斯特电子有限公司 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | POS机办理_个人POS机免费领取 - 银联POS机申请首页 | 台式恒温摇床价格_大容量恒温摇床厂家-上海量壹科学仪器有限公司 | 网站优化公司_北京网站优化_抖音短视频代运营_抖音关键词seo优化排名-通则达网络 | led太阳能路灯厂家价格_风光互补庭院灯_农村市政工程路灯-中山华可路灯品牌 |