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

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

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

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

      <tfoot id='gsWNg'></tfoot>

      如何在 Laravel 中設置 Eloquent 關系屬于通過另一個

      How to set Eloquent relationship belongsTo THROUGH another model in Laravel?(如何在 Laravel 中設置 Eloquent 關系屬于通過另一個模型?)
      <tfoot id='ub6oy'></tfoot>
      • <small id='ub6oy'></small><noframes id='ub6oy'>

          <tbody id='ub6oy'></tbody>
        <legend id='ub6oy'><style id='ub6oy'><dir id='ub6oy'><q id='ub6oy'></q></dir></style></legend>

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

              • 本文介紹了如何在 Laravel 中設置 Eloquent 關系屬于通過另一個模型?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我有一個模型列表,它通過它的belongsTo('Model') 關系繼承應該本質上屬于其對應模型所屬的制造商.

                I have a model Listing that inherits through its belongsTo('Model') relationship should inherently belong to the Manufacturer that its corresponding Model belongs to.

                這是我的列表模型:

                    public function model()
                    {
                        return $this->belongsTo('Model', 'model_id');
                    }
                
                    public function manufacturer()
                    {
                        return $this->belongsTo('Manufacturer', 'models.manufacturer_id');
                        /*
                        $manufacturer_id = $this->model->manufacturer_id;
                        return Manufacturer::find($manufacturer_id)->name;*/
                    }
                

                和我的制造商模型:

                public function listings()
                {
                    return $this->hasManyThrough('Listing', 'Model', 'manufacturer_id', 'model_id');
                }
                
                public function models()
                {
                    return $this->hasMany('Model', 'manufacturer_id');
                }
                

                我可以在視圖中回顯 $listing->model->name,但不能回顯 $listing->manufacturer->name.這會引發錯誤.我嘗試在 Listing 模型中注釋掉 2 行只是為了獲得效果,然后我可以 echo $listing->manufacturer() 并且這會起作用,但這并不能正確建立它們的關系.我該怎么做呢?謝謝.

                I am able to echo $listing->model->name in a view, but not $listing->manufacturer->name. That throws an error. I tried the commented out 2 lines in the Listing model just to get the effect so then I could echo $listing->manufacturer() and that would work, but that doesn't properly establish their relationship. How do I do this? Thanks.

                修訂列表模型(感謝回答者):

                Revised Listing model (thanks to answerer):

                    public function model()
                    {
                        return $this->belongsTo('Model', 'model_id');
                    }
                
                    public function manufacturer()
                    {
                        return $this->belongsTo('Model', 'model_id')
                            ->join('manufacturers', 'manufacturers.id', '=', 'models.manufacturer_id');
                    }
                

                推薦答案

                我找到了一個解決方案,但不是很簡單.我已經在下面發布了它,但我首先發布了我認為更好的解決方案.

                I found a solution, but it's not extremely straight forward. I've posted it below, but I posted what I think is the better solution first.

                您不應該直接從列表中訪問制造商,因為制造商僅適用于模型.雖然您可以從列表對象中預先加載制造商關系,但請參見下文.

                You shouldn't be able to access manufacturer directly from the listing, since manufacturer applies to the Model only. Though you can eager-load the manufacturer relationships from the listing object, see below.

                class Listing extends Eloquent
                {
                    public function model()
                    {
                        return $this->belongsTo('Model', 'model_id');
                    }
                }
                
                class Model extends Eloquent
                {
                    public function manufacturer()
                    {
                        return $this->belongsTo('manufacturer');
                    }
                }
                
                class Manufacturer extends Eloquent
                {
                } 
                
                $listings = Listing::with('model.manufacturer')->all();
                foreach($listings as $listing) {
                    echo $listing->model->name . ' by ' . $listing->model->manufacturer->name;
                }
                

                為了讓您要求的解決方案發揮作用,我們費了一番周折.解決方案如下:

                It took a bit of finagling, to get your requested solution working. The solution looks like this:

                public function manufacturer()
                {
                    $instance = new Manufacturer();
                    $instance->setTable('models');
                    $query = $instance->newQuery();
                
                    return (new BelongsTo($query, $this, 'model_id', $instance->getKeyName(), 'manufacturer'))
                        ->join('manufacturers', 'manufacturers.id', '=', 'models.manufacturer_id')
                        ->select(DB::raw('manufacturers.*'));
                }
                

                我首先處理查詢并從中構建響應.我希望創建的查詢類似于:

                I started off by working with the query and building the response from that. The query I was looking to create was something along the lines of:

                SELECT * FROM manufacturers ma
                    JOIN models m on m.manufacturer_id = ma.id
                WHERE m.id in (?)
                

                通常通過執行 return $this->belongsTo('Manufacturer');

                select * from `manufacturers` where `manufacturers`.`id` in (?)
                

                ? 將替換為列表表中 manufacturer_id 列的值.此列不存在,因此將插入單個 0,并且您永遠不會返回制造商.

                The ? would be replaced by the value of manufacturer_id columns from the listings table. This column doesn't exist, so a single 0 would be inserted and you'd never return a manufacturer.

                在我想重新創建的查詢中,我受到了models.id 的約束.通過定義外鍵,我可以在我的關系中輕松訪問該值.于是關系變成了

                In the query I wanted to recreate I was constraining by models.id. I could easily access that value in my relationship by defining the foreign key. So the relationship became

                return $this->belongsTo('Manufacturer', 'model_id');
                

                這會產生與之前相同的查詢,但會使用 model_ids 填充 ?.所以這會返回結果,但通常是不正確的結果.然后我打算更改我從中選擇的基表.這個值是從模型派生出來的,所以我把傳入的模型改成了Model.

                This produces the same query as it did before, but populates the ? with the model_ids. So this returns results, but generally incorrect results. Then I aimed to change the base table that I was selecting from. This value is derived from the model, so I changed the passed in model to Model.

                return $this->belongsTo('Model', 'model_id');
                

                我們現在已經模擬了模型關系,太好了,我真的什么都沒有.但至少現在,我可以連接到制造商表.所以我再次更新了關系:

                We've now mimic the model relationship, so that's great I hadn't really got anywhere. But at least now, I could make the join to the manufacturers table. So again I updated the relationship:

                return $this->belongsTo('Model', 'model_id')
                    ->join('manufacturers', 'manufacturers.id', '=', 'models.manufacturer_id');
                

                這讓我們更近了一步,生成了以下查詢:

                This got us one step closer, generating the following query:

                select * from `models` 
                    inner join `manufacturers` on `manufacturers`.`id` = `models`.`manufacturer_id`
                    where `models`.`id` in (?)
                

                從這里開始,我想將查詢的列限制為制造商列,為此我添加了選擇規范.這使關系變為:

                From here, I wanted to limit the columns I was querying for to just the manufacturer columns, to do this I added the select specification. This brought the relationship to:

                返回 $this->belongsTo('Model', 'model_id')->加入('制造商','manufacturers.id','=','models.manufacturer_id')->select(DB::raw('manufacturers.*'));

                return $this->belongsTo('Model', 'model_id') ->join('manufacturers', 'manufacturers.id', '=', 'models.manufacturer_id') ->select(DB::raw('manufacturers.*'));

                并得到查詢

                select manufacturers.* from `models` 
                    inner join `manufacturers` on `manufacturers`.`id` = `models`.`manufacturer_id`
                    where `models`.`id` in (?)
                

                現在我們有一個 100% 有效的查詢,但是從關系返回的對象屬于 Model 類型而不是 Manufacturer.這就是最后一點詭計出現的地方.我需要返回一個 Manufacturer,但希望它受到 where 子句中的models表的約束.我創建了一個新的制造商實例并將表設置為models`并手動創建關系.

                Now we have a 100% valid query, but the objects being returned from the relationship are of type Model not Manufacturer. And that's where the last bit of trickery came in. I needed to return a Manufacturer, but wanted it to constrain by themodelstable in the where clause. I created a new instance of Manufacturer and set the table tomodels` and manually create the relationship.

                需要注意的是,保存將不起作用.

                $listing = Listing::find(1);
                $listing->manufacturer()->associate(Manufacturer::create([]));
                $listing->save();
                

                這將創建一個新的制造商,然后將 listings.model_id 更新為新制造商的 ID.

                This will create a new Manufacturer and then update listings.model_id to the new manufacturer's id.

                這篇關于如何在 Laravel 中設置 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 找不到驅動程序)
                • <bdo id='Hp0fO'></bdo><ul id='Hp0fO'></ul>
                    <i id='Hp0fO'><tr id='Hp0fO'><dt id='Hp0fO'><q id='Hp0fO'><span id='Hp0fO'><b id='Hp0fO'><form id='Hp0fO'><ins id='Hp0fO'></ins><ul id='Hp0fO'></ul><sub id='Hp0fO'></sub></form><legend id='Hp0fO'></legend><bdo id='Hp0fO'><pre id='Hp0fO'><center id='Hp0fO'></center></pre></bdo></b><th id='Hp0fO'></th></span></q></dt></tr></i><div class="8vafdwt" id='Hp0fO'><tfoot id='Hp0fO'></tfoot><dl id='Hp0fO'><fieldset id='Hp0fO'></fieldset></dl></div>
                    <legend id='Hp0fO'><style id='Hp0fO'><dir id='Hp0fO'><q id='Hp0fO'></q></dir></style></legend>

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

                          <tbody id='Hp0fO'></tbody>

                      1. <tfoot id='Hp0fO'></tfoot>
                          主站蜘蛛池模板: 模切之家-专注服务模切行业的B2B平台!| 复合肥,化肥厂,复合肥批发,化肥代理,复合肥品牌-红四方 | 彩信群发_群发彩信软件_视频短信营销平台-达信通 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 点胶机_点胶阀_自动点胶机_智能点胶机_喷胶机_点胶机厂家【欧力克斯】 | 上海办公室装修公司_办公室设计_直营办公装修-羚志悦装 | 硅PU球场、篮球场地面施工「水性、环保、弹性」硅PU材料生产厂家-广东中星体育公司 | 全自动真空上料机_粉末真空上料机_气动真空上料机-南京奥威环保科技设备有限公司 | TTCMS自助建站_网站建设_自助建站_免费网站_免费建站_天天向上旗下品牌 | 搪瓷反应釜厂家,淄博搪瓷反应釜-淄博卓耀 | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 小型高低温循环试验箱-可程式高低温湿热交变试验箱-东莞市拓德环境测试设备有限公司 | 胶水,胶粘剂,AB胶,环氧胶,UV胶水,高温胶,快干胶,密封胶,结构胶,电子胶,厌氧胶,高温胶水,电子胶水-东莞聚力-聚厉胶粘 | 江西自考网 | 防伪溯源|防窜货|微信二维码营销|兆信_行业内领先的防伪防窜货数字化营销解决方案供应商 | 恒压供水控制柜|无负压|一体化泵站控制柜|PLC远程调试|MCGS触摸屏|自动控制方案-联致自控设备 | 水平筛厂家-三轴椭圆水平振动筛-泥沙震动筛设备_山东奥凯诺矿机 包装设计公司,产品包装设计|包装制作,包装盒定制厂家-汇包装【官方网站】 | 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 飞象网 - 通信人每天必上的网站| 喷播机厂家_二手喷播机租赁_水泥浆洒布机-河南青山绿水机电设备有限公司 | 免费分销系统 — 分销商城系统_分销小程序开发 -【微商来】 | 锥形螺带干燥机(新型耙式干燥机)百科-常州丰能干燥工程 | 碳刷_刷握_集电环_恒压簧_电刷厂家-上海丹臻机电科技有限公司 | 船用烟火信号弹-CCS防汛救生圈-船用救生抛绳器(海威救生设备) | 法钢特种钢材(上海)有限公司 - 耐磨钢板、高强度钢板销售加工 阀门智能定位器_电液动执行器_气动执行机构-赫尔法流体技术(北京)有限公司 | 新疆乌鲁木齐网站建设-乌鲁木齐网站制作设计-新疆远璨网络 | 深圳善跑体育产业集团有限公司_塑胶跑道_人造草坪_运动木地板 | 北京中航时代-耐电压击穿试验仪厂家-电压击穿试验机 | 拉力机-拉力试验机-万能试验机-电子拉力机-拉伸试验机-剥离强度试验机-苏州皖仪实验仪器有限公司 | 新型锤式破碎机_新型圆锥式_新型颚式破碎机_反击式打沙机_锤式制砂机_青州建源机械 | 强效碱性清洗剂-实验室中性清洗剂-食品级高纯氮气发生器-上海润榕科学器材有限公司 | 成都治疗尖锐湿疣比较好的医院-成都治疗尖锐湿疣那家医院好-成都西南皮肤病医院 | 福建成考网-福建成人高考网| 农产品溯源系统_农产品质量安全追溯系统_溯源系统 | POM塑料_PBT材料「进口」聚甲醛POM杜邦原料、加纤PBT塑料报价格找利隆塑料 | 质检报告_CE认证_FCC认证_SRRC认证_PSE认证_第三方检测机构-深圳市环测威检测技术有限公司 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | 大连海岛旅游网>>大连旅游,大连海岛游,旅游景点攻略,海岛旅游官网 | 焊接烟尘净化器__焊烟除尘设备_打磨工作台_喷漆废气治理设备 -催化燃烧设备 _天津路博蓝天环保科技有限公司 | 聚丙烯酰胺_阴离子_阳离子「用量少」巩义亿腾厂家直销,售后无忧 聚合甘油__盐城市飞龙油脂有限公司 |