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

      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="c0g2sq2" 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="200kk0q" 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="keoaeqi" 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. 主站蜘蛛池模板: led冷热冲击试验箱_LED高低温冲击试验箱_老化试验箱-爱佩百科 | 斗式提升机_链式斗提机_带式斗提机厂家无锡市鸿诚输送机械有限公司 | 无锡市珂妮日用化妆品有限公司|珂妮日化官网|洗手液厂家 | SMC-SMC电磁阀-日本SMC气缸-SMC气动元件展示网 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 山东商品混凝土搅拌楼-环保型搅拌站-拌合站-分体仓-搅拌机厂家-天宇 | 胶泥瓷砖胶,轻质粉刷石膏,嵌缝石膏厂家,腻子粉批发,永康家德兴,永康市家德兴建材厂 | 制氮设备-变压吸附制氮设备-制氧设备-杭州聚贤气体设备制造有限公司 | 智慧旅游_智慧景区_微景通-智慧旅游景区解决方案提供商 | 集装箱箱号识别_自重载重图像识别_铁路车号自动识别_OCR图像识别 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 仿古瓦,仿古金属瓦,铝瓦,铜瓦,铝合金瓦-西安东申景观艺术工程有限公司 | 吸污车_吸粪车_抽粪车_电动三轮吸粪车_真空吸污车_高压清洗吸污车-远大汽车制造有限公司 | 高压负荷开关-苏州雷尔沃电器有限公司| 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 珠海网站建设_响应网站建设_珠海建站公司_珠海网站设计与制作_珠海网讯互联 | 上海平衡机-单面卧式动平衡机-万向节动平衡机-圈带动平衡机厂家-上海申岢动平衡机制造有限公司 | 高速龙门架厂家_监控杆_多功能灯杆_信号灯杆_锂电池太阳能路灯-鑫世源照明 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 西点培训学校_法式西点培训班_西点师培训_西点蛋糕培训-广州烘趣西点烘焙培训学院 | 京港视通报道-质量走进大江南北-京港视通传媒[北京]有限公司 | 真空泵维修保养,普发,阿尔卡特,荏原,卡西亚玛,莱宝,爱德华干式螺杆真空泵维修-东莞比其尔真空机电设备有限公司 | 石牌坊价格石牌坊雕刻制作_石雕牌坊牌楼石栏杆厂家_山东嘉祥石雕有限公司 | 土壤养分检测仪|土壤水分|土壤紧实度测定仪|土壤墒情监测系统-土壤仪器网 | 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 合肥仿石砖_合肥pc砖厂家_合肥PC仿石砖_安徽旭坤建材有限公司 | 升降炉_真空气氛炉_管式电阻炉厂家-山东中辰电炉有限公司 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 基本型顶空进样器-全自动热脱附解吸仪价格-AutoHS全模式-成都科林分析技术有限公司 | 镀锌角钢_槽钢_扁钢_圆钢_方矩管厂家_镀锌花纹板-海邦钢铁(天津)有限公司 | 冷藏车-东风吸污车-纯电动环卫车-污水净化车-应急特勤保障车-程力专汽厂家-程力专用汽车股份有限公司销售二十一分公司 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 招商帮-一站式网络营销服务|互联网整合营销|网络推广代运营|信息流推广|招商帮企业招商好帮手|搜索营销推广|短视视频营销推广 | 膏剂灌装旋盖机-眼药水灌装生产线-西林瓶粉剂分装机-南通博琅机械科技 | 海德莱电力(HYDELEY)-无功补偿元器件生产厂家-二十年专业从事电力电容器 | 客服外包专业服务商_客服外包中心_网萌科技 | 吉祥新世纪铝塑板_生产铝塑板厂家_铝塑板生产厂家_临沂市兴达铝塑装饰材料有限公司 | 螺钉式热电偶_便携式温度传感器_压簧式热电偶|无锡联泰仪表有限公司|首页 | 胜为光纤光缆_光纤跳线_单模尾纤_光纤收发器_ODF光纤配线架厂家直销_北京睿创胜为科技有限公司 - 北京睿创胜为科技有限公司 |