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

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

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

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

        與 Laravel 的友誼系統:多對多關系

        Friendship system with Laravel : Many to Many relationship(與 Laravel 的友誼系統:多對多關系)

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

          • <legend id='JgYQE'><style id='JgYQE'><dir id='JgYQE'><q id='JgYQE'></q></dir></style></legend>
              <tbody id='JgYQE'></tbody>

                  本文介紹了與 Laravel 的友誼系統:多對多關系的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試使用 Laravel 創建一個友誼系統(我是從它開始的),但我被關系阻止了.事情是這樣的:有一張表 Users 和一張表 Friends ,其中包含以下列:

                  I'm trying to create a Friendship system with Laravel (I'm starting with it) but I'm blocked with relationships. Here's the thing : there is one table Users and one table Friends which contains the following columns :

                  friends: id, user_id, friend_id, accepted.
                  

                  它看起來像多對多,所以這是我在用戶類上設置的:

                  It looks like a Many to Many so here's what I set on User class :

                  class User extends Eloquent {
                      function friends()
                      {
                          return $this->belongsToMany('User');
                      }
                  }
                  

                  但是當我嘗試:

                  $friends = User::find($id)->friends()->get()
                  

                  我有這個錯誤:

                  Base table or view not found: 1146 Table 'base.user_user' doesn't exist
                  

                  我想獲取一個用戶的好友列表,無論用戶是發送邀請還是收到邀請.因此,用戶可以根據 user_id 或friend_id 獲取數據,然后根據該列檢索其他用戶的數據.

                  I would like to get a list of the Friends of a user, no matters if the user sent the invitation or received it. So the user can ba on user_id or on friend_id and then I retrieve the data of the other user depending of that column.

                  有什么想法嗎?謝謝!

                  這是我使用的代碼:

                  $usersWithFriends = User::with('friendsOfMine', 'friendOf')->get();
                  $user = User::find(Auth::id())->friends;
                  
                  foreach($user as $item) {
                      echo $item->first()->pivot->accepted;
                  } 
                  

                  推薦答案

                  tldr;您需要 2 個反向關系才能使其工作,請檢查下面的 SETUP 和 USAGE

                  <小時>

                  首先是錯誤 - 這就是你的關系應該是什么樣子:

                  tldr; you need 2 inverted relationships to make it work, check SETUP and USAGE below


                  First off the error - this is how your relation should look like:

                  function friends()
                  {
                    return $this->belongsToMany('User', 'friends', 'user_id', 'friend_id')
                      // if you want to rely on accepted field, then add this:
                      ->wherePivot('accepted', '=', 1);
                  }
                  

                  然后它就會正常工作:

                  $user->friends; // collection of User models, returns the same as:
                  $user->friends()->get();
                  

                  <小時>

                  設置

                  但是您希望關系以兩種方式工作.Eloquent 不提供這種關系,因此您可以改為使用 2 個反向關系并合并結果:


                  SETUP

                  However you would like the relation to work in both ways. Eloquent doesn't provide a relation of that kind, so you can instead use 2 inverted relationships and merge the results:

                  // friendship that I started
                  function friendsOfMine()
                  {
                    return $this->belongsToMany('User', 'friends', 'user_id', 'friend_id')
                       ->wherePivot('accepted', '=', 1) // to filter only accepted
                       ->withPivot('accepted'); // or to fetch accepted value
                  }
                  
                  // friendship that I was invited to 
                  function friendOf()
                  {
                    return $this->belongsToMany('User', 'friends', 'friend_id', 'user_id')
                       ->wherePivot('accepted', '=', 1)
                       ->withPivot('accepted');
                  }
                  
                  // accessor allowing you call $user->friends
                  public function getFriendsAttribute()
                  {
                      if ( ! array_key_exists('friends', $this->relations)) $this->loadFriends();
                  
                      return $this->getRelation('friends');
                  }
                  
                  protected function loadFriends()
                  {
                      if ( ! array_key_exists('friends', $this->relations))
                      {
                          $friends = $this->mergeFriends();
                  
                          $this->setRelation('friends', $friends);
                      }
                  }
                  
                  protected function mergeFriends()
                  {
                      return $this->friendsOfMine->merge($this->friendOf);
                  }
                  

                  用法

                  通過這樣的設置,您可以做到:

                  USAGE

                  With such setup you can do this:

                  // access all friends
                  $user->friends; // collection of unique User model instances
                  
                  // access friends a user invited
                  $user->friendsOfMine; // collection
                  
                  // access friends that a user was invited by
                  $user->friendOf; // collection
                  
                  // and eager load all friends with 2 queries
                  $usersWithFriends = User::with('friendsOfMine', 'friendOf')->get();
                  
                  // then
                  $users->first()->friends; // collection
                  
                  // Check the accepted value:
                  $user->friends->first()->pivot->accepted;
                  

                  這篇關于與 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='6BEal'><style id='6BEal'><dir id='6BEal'><q id='6BEal'></q></dir></style></legend>
                    <i id='6BEal'><tr id='6BEal'><dt id='6BEal'><q id='6BEal'><span id='6BEal'><b id='6BEal'><form id='6BEal'><ins id='6BEal'></ins><ul id='6BEal'></ul><sub id='6BEal'></sub></form><legend id='6BEal'></legend><bdo id='6BEal'><pre id='6BEal'><center id='6BEal'></center></pre></bdo></b><th id='6BEal'></th></span></q></dt></tr></i><div class="kwiyuwo" id='6BEal'><tfoot id='6BEal'></tfoot><dl id='6BEal'><fieldset id='6BEal'></fieldset></dl></div>
                          • <bdo id='6BEal'></bdo><ul id='6BEal'></ul>

                            <small id='6BEal'></small><noframes id='6BEal'>

                              <tbody id='6BEal'></tbody>

                            <tfoot id='6BEal'></tfoot>
                          • 主站蜘蛛池模板: 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | 河南道路标志牌_交通路标牌_交通标志牌厂家-郑州路畅交通 | 不锈钢水箱厂家,不锈钢保温水箱-山东桑特供水设备 | 彩超机-黑白B超机-便携兽用B超机-多普勒彩超机价格「大为彩超」厂家 | 集菌仪厂家_全封闭_封闭式_智能智能集菌仪厂家-上海郓曹 | 胶水,胶粘剂,AB胶,环氧胶,UV胶水,高温胶,快干胶,密封胶,结构胶,电子胶,厌氧胶,高温胶水,电子胶水-东莞聚力-聚厉胶粘 | 磁力抛光机_磁力研磨机_磁力去毛刺机-冠古设备厂家|维修|租赁【官网】 | 高铝轻质保温砖_刚玉莫来石砖厂家_轻质耐火砖价格 | 执业药师报名条件,考试时间,考试真题,报名入口—首页 | 市政路灯_厂家-淄博信达电力科技有限公司 | 振动筛,震动筛,圆形振动筛,振动筛价格,振动筛厂家-新乡巨宝机电 蒸汽热收缩机_蒸汽发生器_塑封机_包膜机_封切收缩机_热收缩包装机_真空机_全自动打包机_捆扎机_封箱机-东莞市中堡智能科技有限公司 | wika威卡压力表-wika压力变送器-德国wika代理-威卡总代-北京博朗宁科技 | 辐射色度计-字符亮度测试-反射式膜厚仪-苏州瑞格谱光电科技有限公司 | 防渗膜厂家|养殖防渗膜|水产养殖防渗膜-泰安佳路通工程材料有限公司 | 昊宇水工|河北昊宇水工机械工程有限公司 | 逗网红-抖音网红-快手网红-各大平台网红物品导航 | 学校用栓剂模,玻璃瓶轧盖钳,小型安瓿熔封机,实验室安瓿熔封机-长沙中亚制药设备有限公司 | 间甲酚,间甲酚厂家-山东祥东新材料 | 盘古网络技术有限公司| HDPE土工膜,复合土工膜,防渗膜价格,土工膜厂家-山东新路通工程材料有限公司 | 冷库安装厂家_杭州冷库_保鲜库建设-浙江克冷制冷设备有限公司 | 泥浆在线密度计厂家-防爆数字压力表-膜盒-远传压力表厂家-江苏大亚自控设备有限公司 | 台式低速离心机-脱泡离心机-菌种摇床-常州市万丰仪器制造有限公司 | MVR蒸发器厂家-多效蒸发器-工业废水蒸发器厂家-康景辉集团官网 | 活性氧化铝|无烟煤滤料|活性氧化铝厂家|锰砂滤料厂家-河南新泰净水材料有限公司 | 在线钠离子分析仪-硅酸根离子浓度测定仪-油液水分测定仪价格-北京时代新维测控设备有限公司 | 北京京云律师事务所| 安徽华耐泵阀有限公司-官方网站| 【黄页88网】-B2B电子商务平台,b2b平台免费发布信息网 | 珠光砂保温板-一体化保温板-有釉面发泡陶瓷保温板-杭州一体化建筑材料 | 西门子伺服电机维修,西门子电源模块维修,西门子驱动模块维修-上海渠利 | 磁力反应釜,高压釜,实验室反应釜,高温高压反应釜-威海自控反应釜有限公司 | 进口便携式天平,外校_十万分之一分析天平,奥豪斯工业台秤,V2000防水秤-重庆珂偌德科技有限公司(www.crdkj.com) | 吸污车_吸粪车_抽粪车_电动三轮吸粪车_真空吸污车_高压清洗吸污车-远大汽车制造有限公司 | 深圳美安可自动化设备有限公司,喷码机,定制喷码机,二维码喷码机,深圳喷码机,纸箱喷码机,东莞喷码机 UV喷码机,日期喷码机,鸡蛋喷码机,管芯喷码机,管内壁喷码机,喷码机厂家 | 合肥制氮机_合肥空压机厂家_安徽真空泵-凯圣精机 | 莱州网络公司|莱州网站建设|莱州网站优化|莱州阿里巴巴-莱州唯佳网络科技有限公司 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | WTB5光栅尺-JIE WILL磁栅尺-B60数显表-常州中崴机电科技有限公司 | 东莞ERP软件_广州云ERP_中山ERP_台湾工厂erp系统-广东顺景软件科技有限公司 | 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 |