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

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

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

      <tfoot id='JXUW6'></tfoot>

    1. <legend id='JXUW6'><style id='JXUW6'><dir id='JXUW6'><q id='JXUW6'></q></dir></style></legend>

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

      Laravel 多對多自引用表只能以一種方式工作

      Laravel Many to many self referencing table only works one way(Laravel 多對多自引用表只能以一種方式工作)

      <small id='2sNhG'></small><noframes id='2sNhG'>

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

            <tfoot id='2sNhG'></tfoot>
              <tbody id='2sNhG'></tbody>
          1. <legend id='2sNhG'><style id='2sNhG'><dir id='2sNhG'><q id='2sNhG'></q></dir></style></legend>
              <bdo id='2sNhG'></bdo><ul id='2sNhG'></ul>
              • 本文介紹了Laravel 多對多自引用表只能以一種方式工作的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我已經建立了關系和模型如下:

                I have set up the relationship and the models as below:

                數據透視表架構

                Schema::create('friend_user', function(Blueprint $table) {
                    $table->increments('id');
                    $table->integer('user_id')->unsigned();
                    $table->integer('friend_id')->unsigned();
                    $table->timestamps();
                });
                

                數據透視表播種器(這定義了用戶1"所屬的兩個友誼",一個是在 user_id 中指定了用戶 1,另一個是在朋友 ID 中列出了用戶 1):

                pivot table seeder (this defines two 'friendships' to which user '1' belongs one where user 1 is specified in the user_id and the second where user 1 is listed in the friend id):

                    $friend_user = array(
                        array(
                            'id' => 1,
                            'user_id' => 1,
                            'friend_id' => 3,
                        ),
                        array(
                            'id' => 2,
                            'user_id' => 4,
                            'friend_id' => 1,
                        ),
                
                    );
                

                用戶模型

                public function friends()
                {
                 return $this->belongsToMany('User', 'friend_user', 'user_id', 'friend_id');
                }
                

                這是 Taylor Otwell 在這里建議的:https://github.com/laravel/framework/issues/441

                This is as suggested by Taylor Otwell here: https://github.com/laravel/framework/issues/441

                這一切都有效,但是當我運行以下命令時,我只得到一個結果:

                This all works but when I run the following command I get only one result:

                foreach(Auth::user()->friends as $i) {
                    var_dump($i->id);
                }
                

                這將返回值 '3' 但不是預期的 4.我理解為什么會發生這種情況(因為 user_id 不是friend_id)但是我怎樣才能讓它返回屬于一個用戶的所有朋友(即所有朋友)的集合,而不管連接的哪一端(user_id 或friend_id)用戶是?

                This returns the value '3' but not 4 as expected. I understand why this is happening (as the user_id is not the friend_id) but how can I get this to return a collection of all friends belonging to a user (ie all friendships) regardless of which end of the connection (user_id or friend_id) the user is?

                推薦答案

                使用新函數代替創建兩條記錄.

                Instead of creating two records use a new function.

                public function friends()
                {
                  return $this->belongsToMany('User', 'friend_user', 'user_id', 'friend_id');
                }
                
                // Same table, self referencing, but change the key order
                public function theFriends()
                {
                  return $this->belongsToMany('User', 'friend_user', 'friend_id', 'user_id');
                }
                
                //You can then call opposite record(s) using:
                foreach( Auth::user()->theFriends as $theFriends )
                

                我在我的項目中使用了這種方法,因此我可以更好地分離組織結果.

                I used this approach in my project so I can have better separation for organizing the results.

                這篇關于Laravel 多對多自引用表只能以一種方式工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 找不到驅動程序)

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

                    <tfoot id='FvyZq'></tfoot>

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

                    <i id='FvyZq'><tr id='FvyZq'><dt id='FvyZq'><q id='FvyZq'><span id='FvyZq'><b id='FvyZq'><form id='FvyZq'><ins id='FvyZq'></ins><ul id='FvyZq'></ul><sub id='FvyZq'></sub></form><legend id='FvyZq'></legend><bdo id='FvyZq'><pre id='FvyZq'><center id='FvyZq'></center></pre></bdo></b><th id='FvyZq'></th></span></q></dt></tr></i><div class="w0y02u2" id='FvyZq'><tfoot id='FvyZq'></tfoot><dl id='FvyZq'><fieldset id='FvyZq'></fieldset></dl></div>
                      <bdo id='FvyZq'></bdo><ul id='FvyZq'></ul>
                        <tbody id='FvyZq'></tbody>
                          主站蜘蛛池模板: 仿真茅草_人造茅草瓦价格_仿真茅草厂家_仿真茅草供应-深圳市科佰工贸有限公司 | 北京银联移动POS机办理_收银POS机_智能pos机_刷卡机_收银系统_个人POS机-谷骐科技【官网】 | 深圳宣传片制作-企业宣传视频制作-产品视频拍摄-产品动画制作-短视频拍摄制作公司 | 智慧水务|智慧供排水利信息化|水厂软硬件系统-上海敢创 | 五轴加工中心_数控加工中心_铝型材加工中心-罗威斯 | 仪器仪表网 - 永久免费的b2b电子商务平台 | 防堵吹扫装置-防堵风压测量装置-电动操作显示器-兴洲仪器 | 阜阳成人高考_阜阳成考报名时间_安徽省成人高考网 | SF6环境监测系统-接地环流在线监测装置-瑟恩实业 | 安全,主动,被动,柔性,山体滑坡,sns,钢丝绳,边坡,防护网,护栏网,围栏,栏杆,栅栏,厂家 - 护栏网防护网生产厂家 | 除甲醛公司-甲醛检测治理-杭州创绿家环保科技有限公司-室内空气净化十大品牌 | 工业rfid读写器_RFID工业读写器_工业rfid设备厂商-ANDEAWELL | 隧道窑炉,隧道窑炉厂家-山东艾瑶国际贸易| 电机修理_二手电机专家-河北豫通机电设备有限公司(原石家庄冀华高压电机维修中心) | 莱州网络公司|莱州网站建设|莱州网站优化|莱州阿里巴巴-莱州唯佳网络科技有限公司 | 结晶点测定仪-润滑脂滴点测定仪-大连煜烁 | 无锡网站建设-做网站-建网站-网页设计制作-阿凡达建站公司 | 猎头招聘_深圳猎头公司_知名猎头公司 | Copeland/谷轮压缩机,谷轮半封闭压缩机,谷轮涡旋压缩机,型号规格,技术参数,尺寸图片,价格经销商 CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | 众品地板网-地板品牌招商_地板装修设计_地板门户的首选网络媒体。 | 螺纹三通快插接头-弯通快插接头-宁波舜驰气动科技有限公司 | 灌装封尾机_胶水灌装机_软管灌装封尾机_无锡和博自动化机械制造有限公司 | RV减速机-蜗轮蜗杆减速机-洗车机减速机-减速机厂家-艾思捷 | 苏商学院官网 - 江苏地区唯一一家企业家自办的前瞻型、实操型商学院 | 没斑啦-专业的祛斑美白嫩肤知识网站-去斑经验分享 | 【德信自动化】点胶机_全自动点胶机_自动点胶机厂家_塑料热压机_自动螺丝机-深圳市德信自动化设备有限公司 | 杰恒蠕动泵-蠕动泵专业厂家-19年专注蠕动泵 | 高精度-恒温冷水机-螺杆式冰水机-蒸发冷冷水机-北京蓝海神骏科技有限公司 | 多功能真空滤油机_润滑油全自动滤油机_高效真空滤油机价格-重庆润华通驰 | 连栋温室大棚建造厂家-智能玻璃温室-薄膜温室_青州市亿诚农业科技 | 对夹式止回阀厂家,温州对夹式止回阀制造商--永嘉县润丰阀门有限公司 | 钣金加工厂家-钣金加工-佛山钣金厂-月汇好 | 潜水搅拌机-双曲面搅拌机-潜水推进器|奥伯尔环保 | 光纤测温-荧光光纤测温系统-福州华光天锐光电科技有限公司 | 长沙广告公司|长沙广告制作设计|长沙led灯箱招牌制作找望城湖南锦蓝广告装饰工程有限公司 | 高压包-点火器-高压发生器-点火变压器-江苏天网 | 无锡装修装潢公司,口碑好的装饰装修公司-无锡索美装饰设计工程有限公司 | 无菌水质袋-NASCO食品无菌袋-Whirl-Pak无菌采样袋-深圳市慧普德贸易有限公司 | led全彩屏-室内|学校|展厅|p3|户外|会议室|圆柱|p2.5LED显示屏-LED显示屏价格-LED互动地砖屏_蕙宇屏科技 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 智能风向风速仪,风速告警仪,数字温湿仪,综合气象仪(气象五要素)-上海风云气象仪器有限公司 |