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

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

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

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

      <tfoot id='nS9Pu'></tfoot>

      自引用表上的 Laravel Eloquent Inner Join

      Laravel Eloquent Inner Join on Self Referencing Table(自引用表上的 Laravel Eloquent Inner Join)
        • <bdo id='pzgeq'></bdo><ul id='pzgeq'></ul>
          1. <i id='pzgeq'><tr id='pzgeq'><dt id='pzgeq'><q id='pzgeq'><span id='pzgeq'><b id='pzgeq'><form id='pzgeq'><ins id='pzgeq'></ins><ul id='pzgeq'></ul><sub id='pzgeq'></sub></form><legend id='pzgeq'></legend><bdo id='pzgeq'><pre id='pzgeq'><center id='pzgeq'></center></pre></bdo></b><th id='pzgeq'></th></span></q></dt></tr></i><div class="z5jlf77" id='pzgeq'><tfoot id='pzgeq'></tfoot><dl id='pzgeq'><fieldset id='pzgeq'></fieldset></dl></div>

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

              <tfoot id='pzgeq'></tfoot>

                  <tbody id='pzgeq'></tbody>

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

                本文介紹了自引用表上的 Laravel Eloquent Inner Join的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我正在嘗試使用雄辯的模型將用戶表內部連接到自身.我到處尋找,但似乎無法找到解決此不創建兩個查詢的解決方案,這正是我目前正在做的事情.

                I'm trying to inner join a users table to itself using an eloquent model. I've looked everywhere but can't seem to find a solution to this without creating two queries which is what I am currently doing.

                users 表通過 pivotfriends

                我嘗試將 Users::class 內部連接到自身,但失敗了.在內部連接中我能得到的最好結果是運行兩個查詢并查看是否有重疊.因此,一個人聯系了另一個人,反之亦然.

                I tried and failed inner joining Users::class to itself. The best I can get at an inner join is by running two queries and seeing if there is an overlap. Thus one person has reached out to the other and vice versa.

                friends   | users
                ----------|------
                send_id   | id
                receive_id| name
                is_blocked|
                

                樣本數據 &預期結果

                users.id | name
                ---------|------
                1        | foo
                2        | bar
                3        | baz
                
                friends
                send_id | receive_id | is_blocked
                --------|------------|-----------
                1       |    2       |  0
                2       |    1       |  0
                1       |    3       |  0
                3       |    1       |  1
                2       |    3       |  0
                

                用戶應該有一種稱為朋友的雄辯關系.它應該是您期望的來自 requestedFriendsreceivedFriends 剛剛加入的結果.

                The user should have an eloquent relationship called friends. It should be what you expect comes out of requestedFriends or receivedFriends just joined.

                foo->friends
                returns `baz`
                bar->friends
                returns `foo`
                baz->friends
                returns empty collection
                
                

                正在使用

                // User.php
                public function requestedFriends()
                {
                    $left = $this->belongsToMany(User::class, 'friends','send_id','receive_id')
                        ->withPivot('is_blocked')
                        ->wherePivot('is_blocked','=', 0)
                        ->withTimestamps();
                    return $left;
                }
                
                public function receivedFriends()
                {
                    $right = $this->belongsToMany(User::class, 'friends','receive_id','send_id')
                        ->withPivot('is_blocked')
                        ->wherePivot('is_blocked','=', 0)
                        ->withTimestamps();
                
                    return $right;
                }
                
                public function friends()
                {
                    $reqFriends = $this->requestedFriends()->get();
                    $recFriends = $this->receivedFriends()->get();
                    $req = explode(",",$recFriends->implode('id', ', '));
                    $intersect = $reqFriends->whereIn('id', $req);
                    return $intersect;
                }
                

                目前的研究

                Laravel 多對多自引用表只能以一種方式工作 ->老問題,但仍然相關

                Research so far

                Laravel Many to many self referencing table only works one way -> old question, but still relevant

                https://github.com/laravel/framework/issues/441#issuecomment-14213883 ->是的,它有效……但只有一種方式.

                https://github.com/laravel/framework/issues/441#issuecomment-14213883 -> yep, it works… but one way.

                https://laravel.com/docs/5.8/collections#method-wherein目前我找到的唯一方法是雄辯地做到這一點.

                https://laravel.com/docs/5.8/collections#method-wherein currently the only way I have found to do this in eloquent.

                https://laravel.com/docs/5.7/queries#joins->理想情況下我會找到一個在自身上使用內連接的解決方案,但無論我用哪種方式放置 ID,我都無法找到解決方案.

                https://laravel.com/docs/5.7/queries#joins -> Ideally I would find a solution using an innerjoin onto itself, but no matter which way I put the id's I couldn't get a solution to work.

                一個解決方案是在 laravel 5.75.8 中使用 eloquent 來內部連接自引用表,其中關系僅在 send_id & 時存在.receive_id 出現在朋友表的多行中.

                A solution would inner join a self referencing table using eloquent in laravel 5.7 or 5.8, where a relationship only exists if send_id & receive_id are present on multiple rows in the friends table.

                以某種方式讓社區知道這是不可能的.

                Somehow let the community know that this can't be done.

                提前致謝!

                推薦答案

                我還沒有詳細檢查這個解決方案,但我已經寫了一個ManyToMany"類擴展了 laravel 附帶的BelongsToMany"類,它似乎工作.該類基本上只是覆蓋了get"方法,復制了原始查詢,反轉"了它,并對原始查詢執行了聯合".

                I have not checked this solution in every detail yet, but I have written a "ManyToMany" Class extending the "BelongsToMany" Class shipped with laravel, which appears to work. The class basically just overrides the "get" method, duplicating the original query, "inverting" it and just performing a "union" on the original query.

                <?php
                
                namespace AppDatabaseEloquentRelations;
                
                use IlluminateDatabaseEloquentRelationsBelongsToMany;
                
                class ManyToMany extends BelongsToMany
                {
                
                    /**
                     * Execute the query as a "select" statement.
                     *
                     * @param  array  $columns
                     * @return IlluminateDatabaseEloquentCollection
                     */
                    public function get($columns = ['*'])
                    {
                        // duplicated from "BelongsToMany"
                        $builder = $this->query->applyScopes();
                
                        $columns = $builder->getQuery()->columns ? [] : $columns;
                
                        // Adjustments for "Many to Many on self": do not get the resulting models here directly, but rather
                        // just set the columns to select and do some adjustments to also select the "inverse" records
                        $builder->addSelect(
                            $this->shouldSelect($columns)
                        );
                
                        // backup order directives
                        $orders = $builder->getQuery()->orders;
                        $builder->getQuery()->orders = [];
                
                        // clone the original query
                        $query2 = clone($this->query);
                
                        // determine the columns to select - same as in original query, but with inverted pivot key names
                        $query2->select(
                            $this->shouldSelectInverse( $columns )
                        );
                        // remove the inner join and build a new one, this time using the "foreign" pivot key
                        $query2->getQuery()->joins = array();
                
                        $baseTable = $this->related->getTable();
                        $key = $baseTable.'.'.$this->relatedKey;
                        $query2->join($this->table, $key, '=', $this->getQualifiedForeignPivotKeyName());
                
                        // go through all where conditions and "invert" the one relevant for the inner join
                        foreach( $query2->getQuery()->wheres as &$where ) {
                            if(
                                $where['type'] == 'Basic'
                                && $where['column'] == $this->getQualifiedForeignPivotKeyName()
                                && $where['operator'] == '='
                                && $where['value'] == $this->parent->{$this->parentKey}
                            ) {
                                $where['column'] = $this->getQualifiedRelatedPivotKeyName();
                                break;
                            }
                        }
                
                        // add the duplicated and modified and adjusted query to the original query with union
                        $builder->getQuery()->union($query2);
                
                        // reapply orderings so that they are used for the "union" rather than just the individual queries
                        foreach($orders as $ord)
                            $builder->getQuery()->orderBy($ord['column'], $ord['direction']);
                
                        // back to "normal" - get the models
                        $models = $builder->getModels();
                        $this->hydratePivotRelation($models);
                
                        // If we actually found models we will also eager load any relationships that
                        // have been specified as needing to be eager loaded. This will solve the
                        // n + 1 query problem for the developer and also increase performance.
                        if (count($models) > 0) {
                            $models = $builder->eagerLoadRelations($models);
                        }
                
                        return $this->related->newCollection($models);
                    }
                
                
                    /**
                     * Get the select columns for the relation query.
                     *
                     * @param  array  $columns
                     * @return array
                     */
                    protected function shouldSelectInverse(array $columns = ['*'])
                    {
                        if ($columns == ['*']) {
                            $columns = [$this->related->getTable().'.*'];
                        }
                
                        return array_merge($columns, $this->aliasedPivotColumnsInverse());
                    }
                
                    /**
                     * Get the pivot columns for the relation.
                     *
                     * "pivot_" is prefixed ot each column for easy removal later.
                     *
                     * @return array
                     */
                    protected function aliasedPivotColumnsInverse()
                    {
                        $collection = collect( $this->pivotColumns )->map(function ($column) {
                            return $this->table.'.'.$column.' as pivot_'.$column;
                        });
                        $collection->prepend(
                            $this->table.'.'.$this->relatedPivotKey.' as pivot_'.$this->foreignPivotKey
                        );
                        $collection->prepend(
                            $this->table.'.'.$this->foreignPivotKey.' as pivot_'.$this->relatedPivotKey
                        );
                
                        return $collection->unique()->all();
                    }
                
                }
                

                這篇關于自引用表上的 Laravel Eloquent Inner Join的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)

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

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

                            <tbody id='a5Fmc'></tbody>
                          主站蜘蛛池模板: POS机办理_个人POS机免费领取 - 银联POS机申请首页 | 郑州巴特熔体泵有限公司专业的熔体泵,熔体齿轮泵与换网器生产厂家 | 全自动真空上料机_粉末真空上料机_气动真空上料机-南京奥威环保科技设备有限公司 | 武汉高温老化房,恒温恒湿试验箱,冷热冲击试验箱-武汉安德信检测设备有限公司 | 悬浮拼装地板_篮球场木地板翻新_运动木地板价格-上海越禾运动地板厂家 | 天津仓库出租网-天津电商仓库-天津云仓一件代发-【博程云仓】 | 包装设计公司,产品包装设计|包装制作,包装盒定制厂家-汇包装【官方网站】 | 在线钠离子分析仪-硅酸根离子浓度测定仪-油液水分测定仪价格-北京时代新维测控设备有限公司 | 湖州织里童装_女童男童中大童装_款式多尺码全_织里儿童网【官网】-嘉兴嘉乐网络科技有限公司 | 深圳善跑体育产业集团有限公司_塑胶跑道_人造草坪_运动木地板 | 茶楼装修设计_茶馆室内设计效果图_云臻轩茶楼装饰公司 | 浙江栓钉_焊钉_剪力钉厂家批发_杭州八建五金制造有限公司 | 【直乐】河北石家庄脊柱侧弯医院_治疗椎间盘突出哪家医院好_骨科脊柱外科专业医院_治疗抽动症/关节病骨伤权威医院|排行-直乐矫形中医医院 | 点胶机_点胶阀_自动点胶机_智能点胶机_喷胶机_点胶机厂家【欧力克斯】 | 地脚螺栓_材质_标准-永年县德联地脚螺栓厂家| 杭州|上海贴标机-百科 | 14米地磅厂家价价格,150吨地磅厂家价格-百科 | 泵阀展|阀门展|水泵展|流体机械展 -2025上海国际泵管阀展览会flowtech china | 玻璃钢罐_玻璃钢储罐_盐酸罐厂家-河北华盛节能设备有限公司 | Magnescale探规,Magnescale磁栅尺,Magnescale传感器,Magnescale测厚仪,Mitutoyo光栅尺,笔式位移传感器-苏州连达精密量仪有限公司 | 隧道风机_DWEX边墙风机_SDS射流风机-绍兴市上虞科瑞风机有限公司 | 机房监控|动环监控|动力环境监控系统方案产品定制厂家 - 迈世OMARA | 工业用品一站式采购平台|南创工品汇-官网|广州南创 | 新能源汽车电池软连接,铜铝复合膜柔性连接,电力母排-容发智能科技(无锡)有限公司 | 桁架机器人_桁架机械手_上下料机械手_数控车床机械手-苏州清智科技装备制造有限公司 | 医学动画公司-制作3d医学动画视频-医疗医学演示动画制作-医学三维动画制作公司 | 广州办公室设计,办公室装修,写字楼设计,办公室装修公司_德科 | 首页-恒温恒湿试验箱_恒温恒湿箱_高低温试验箱_高低温交变湿热试验箱_苏州正合 | 北京包装设计_标志设计公司_包装设计公司-北京思逸品牌设计 | 菏泽商标注册_菏泽版权登记_商标申请代理_菏泽商标注册去哪里 | 吸音板,隔音板,吸音材料,吸音板价格,声学材料 - 佛山诺声吸音板厂家 | 环讯传媒,永康网络公司,永康网站建设,永康小程序开发制作,永康网站制作,武义网页设计,金华地区网站SEO优化推广 - 永康市环讯电子商务有限公司 | H型钢切割机,相贯线切割机,数控钻床,数控平面钻,钢结构设备,槽钢切割机,角钢切割机,翻转机,拼焊矫一体机 | 原色会计-合肥注册公司_合肥代理记账公司_营业执照代办 | 阜阳在线-阜阳综合门户| 称重传感器,测力传感器,拉压力传感器,压力变送器,扭矩传感器,南京凯基特电气有限公司 | SRRC认证_电磁兼容_EMC测试整改_FCC认证_SDOC认证-深圳市环测威检测技术有限公司 | 回转窑-水泥|石灰|冶金-巩义市瑞光金属制品有限责任公司 | 即用型透析袋,透析袋夹子,药敏纸片,L型涂布棒-上海桥星贸易有限公司 | 铁艺,仿竹,竹节,护栏,围栏,篱笆,栅栏,栏杆,护栏网,网围栏,厂家 - 河北稳重金属丝网制品有限公司 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | 吲哚菁绿衍生物-酶底物法大肠菌群检测试剂-北京和信同通科技发展有限公司 |