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

      1. <small id='uV0N0'></small><noframes id='uV0N0'>

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

        <i id='uV0N0'><tr id='uV0N0'><dt id='uV0N0'><q id='uV0N0'><span id='uV0N0'><b id='uV0N0'><form id='uV0N0'><ins id='uV0N0'></ins><ul id='uV0N0'></ul><sub id='uV0N0'></sub></form><legend id='uV0N0'></legend><bdo id='uV0N0'><pre id='uV0N0'><center id='uV0N0'></center></pre></bdo></b><th id='uV0N0'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='uV0N0'><tfoot id='uV0N0'></tfoot><dl id='uV0N0'><fieldset id='uV0N0'></fieldset></dl></div>
          <bdo id='uV0N0'></bdo><ul id='uV0N0'></ul>
      2. 獲取 Eloquent 模型的關系數組

        Get array of Eloquent model#39;s relations(獲取 Eloquent 模型的關系數組)
        <i id='rAuZr'><tr id='rAuZr'><dt id='rAuZr'><q id='rAuZr'><span id='rAuZr'><b id='rAuZr'><form id='rAuZr'><ins id='rAuZr'></ins><ul id='rAuZr'></ul><sub id='rAuZr'></sub></form><legend id='rAuZr'></legend><bdo id='rAuZr'><pre id='rAuZr'><center id='rAuZr'></center></pre></bdo></b><th id='rAuZr'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='rAuZr'><tfoot id='rAuZr'></tfoot><dl id='rAuZr'><fieldset id='rAuZr'></fieldset></dl></div>
        <legend id='rAuZr'><style id='rAuZr'><dir id='rAuZr'><q id='rAuZr'></q></dir></style></legend>
          <tbody id='rAuZr'></tbody>

          <tfoot id='rAuZr'></tfoot>

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

            1. <small id='rAuZr'></small><noframes id='rAuZr'>

                • 本文介紹了獲取 Eloquent 模型的關系數組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試獲取所有模型關聯的數組.我有以下模型:

                  I'm trying to get an array of all of my model's associations. I have the following model:

                  class Article extends Eloquent 
                  {
                      protected $guarded = array();
                  
                      public static $rules = array();
                  
                      public function author() 
                      {
                          return $this->belongsTo('Author');
                      }
                  
                      public function category() 
                      {
                          return $this->belongsTo('Category');
                      }
                  }
                  

                  從這個模型中,我試圖獲得以下關系數組:

                  From this model, I'm trying to get the following array of its relations:

                  array(
                      'author',
                      'category'
                  )
                  

                  我正在尋找一種方法來自動從模型中拉出這個數組.

                  I'm looking for a way to pull this array out from the model automatically.

                  我找到了這個定義Eloquent 模型上的relationsToArray 方法,它似乎返回模型關系的數組.它似乎使用了 Eloquent 模型的 $this->relations 屬性.然而,這個方法返回一個空數組,并且關系屬性是一個空數組,盡管我的關系設置正確.

                  I've found this definition of a relationsToArray method on an Eloquent model, which appears to return an array of the model's relations. It seems to use the $this->relations attribute of the Eloquent model. However, this method returns an empty array, and the relations attribute is an empty array, despite having my relations set up correctly.

                  如果不存儲模型關系,$this->relations 有什么用?有什么辦法可以自動獲取我的模型關系數組?

                  What is $this->relations used for if not to store model relations? Is there any way that I can get an array of my model's relations automatically?

                  推薦答案

                  這是不可能的,因為只有在使用 with(用于急切加載)或使用定義的關系公共方法請求時才加載關系模型,例如,如果一個 Author 模型是用以下關系創建的

                  It's not possible because relationships are loaded only when requested either by using with (for eager loading) or using relationship public method defined in the model, for example, if a Author model is created with following relationship

                  public function articles() {
                      return $this->hasMany('Article');
                  }
                  

                  當你像這樣調用這個方法時:

                  When you call this method like:

                  $author = Author::find(1);
                  $author->articles; // <-- this will load related article models as a collection
                  

                  另外,正如我所說的with,當你使用這樣的東西時:

                  Also, as I said with, when you use something like this:

                  $article = Article::with('author')->get(1);
                  

                  在這種情況下,第一篇文章(id為1)將加載其相關模型Author,您可以使用

                  In this case, the first article (with id 1) will be loaded with it's related model Author and you can use

                  $article->author->name; // to access the name field from related/loaded author model
                  

                  因此,如果不使用適當的方法加載關系,就不可能神奇地獲得關系,但是一旦加載了關系(相關模型),您就可以使用這樣的方法來獲取關系:

                  So, it's not possible to get the relations magically without using appropriate method for loading of relationships but once you load the relationship (related models) then you may use something like this to get the relations:

                  $article = Article::with(['category', 'author'])->first();
                  $article->getRelations(); // get all the related models
                  $article->getRelation('author'); // to get only related author model
                  

                  要將它們轉換為 array,您可以使用 toArray() 方法,例如:

                  To convert them to an array you may use toArray() method like:

                  dd($article->getRelations()->toArray()); // dump and die as array
                  

                  relationsToArray() 方法適用于加載了相關模型的模型.該方法將相關模型轉換為數組形式,其中toArray()方法將模型(有關系)的所有數據轉換為數組,源代碼如下:

                  The relationsToArray() method works on a model which is loaded with it's related models. This method converts related models to array form where toArray() method converts all the data of a model (with relationship) to array, here is the source code:

                  public function toArray()
                  {
                       $attributes = $this->attributesToArray();
                  
                       return array_merge($attributes, $this->relationsToArray());
                  }
                  

                  將模型屬性和與其相關的模型屬性合并成數組后返回.

                  It merges model attributes and it's related model's attributes after converting to array then returns it.

                  這篇關于獲取 Eloquent 模型的關系數組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 找不到驅動程序)

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

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

                    • <legend id='o16s7'><style id='o16s7'><dir id='o16s7'><q id='o16s7'></q></dir></style></legend>
                        <tbody id='o16s7'></tbody>
                        <bdo id='o16s7'></bdo><ul id='o16s7'></ul>

                          1. 主站蜘蛛池模板: 青久草视频 | 亚洲国产精品成人无久久精品 | 亚洲免费精品 | 国产成人在线播放 | 亚洲人久久 | 欧美一级在线 | 国产成人精品网站 | 国内精品99| 精品欧美视频 | 国产成人综合一区二区三区 | www.v888av.com| 日本一区视频在线观看 | 日韩精品在线视频免费观看 | 国产福利视频网站 | 偷牌自拍| 亚洲日日夜夜 | 国产高清一区 | 精品国产欧美一区二区三区成人 | 一级片免费网站 | 欧美理论| 亚洲午夜视频在线观看 | 亚洲成人精品 | 国产97碰免费视频 | 亚洲第1页 | 国产精品乱码一区二三区小蝌蚪 | 夜夜操天天艹 | 亚洲一区二区三区在线视频 | 成年免费大片黄在线观看岛国 | 国产成人小视频 | 成人特级毛片 | 99免费精品视频 | 91精品国产一区二区在线观看 | 欧美久久久久久久久中文字幕 | 午夜免费视频 | 日本不卡一区二区 | 欧美日韩在线看 | 欧美视频一区二区三区 | 色视频网站 | 国产在线一区二区三区 | 精品国产一区二区三区性色av | 久久1区 |