久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <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="qwawimqqmiuu" 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="qwawimqqmiuu" 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="qwawimqqmiuu" 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>
                            主站蜘蛛池模板: 亚洲一区中文字幕在线观看 | 日韩免费av | 国产成人啪免费观看软件 | 丝袜毛片| 在线看片国产 | 欧美 日韩 国产 成人 在线 | 精品免费国产一区二区三区四区 | 中文字幕在线播放第一页 | 国精产品一品二品国精在线观看 | 亚洲精品久久久久久一区二区 | 99久久久久久久 | 91在线资源| 久久精品视频网站 | 一区二区三区av | 久久久爽爽爽美女图片 | 一区二区视频在线 | 国产精品成人一区二区三区 | 日日夜夜av | 亚洲av毛片成人精品 | 色在线视频网站 | 一区二区三区欧美 | 91国内精精品久久久久久婷婷 | 国产精品人人做人人爽 | 九九av| 91精品国产综合久久久久久丝袜 | 在线国产视频 | 国产一区免费视频 | 射欧美 | 日本三级电影在线观看视频 | 国产一级片一区二区 | 美女视频一区二区三区 | 国产网站在线 | 日韩一二区 | 麻豆成人在线视频 | 欧美高清性xxxxhdvideosex | 精品视频在线观看 | 久久精品色视频 | 国产一区二区观看 | 欧美一级在线免费观看 | 国产精品99久久久久久久vr | 久久久www成人免费无遮挡大片 |