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

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

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

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

      有一個(gè)通過 Laravel Eloquent

      Has one through Laravel Eloquent(有一個(gè)通過 Laravel Eloquent)
          <tbody id='fTCZp'></tbody>

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

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

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

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

              • 本文介紹了有一個(gè)通過 Laravel Eloquent的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我有三個(gè)表garages、carssecurities.

                證券是保護(hù)一輛汽車的安全,您可以擁有多個(gè)安全,但一個(gè)安全只能保護(hù)一輛車.車庫是汽車所在,證券也是.

                我想要的是列出所有證券并知道他所在的車庫的名稱.問題是 securities 沒有包含車庫 id 的列,只有他保持安全的汽車的 id,但是 car 有車庫的 id.

                Laravel Eloquent 有一個(gè)名為 hasManyThrough 的方法,但 securities 只有一個(gè) garagecars.>

                這是表格:

                車庫表:

                -----------------------------------|車庫編號|車庫名稱|車庫大小|---------------------|1|車庫 1|200|---------------------|2|車庫 2|400|---------------------

                汽車表:

                ---------------------------|car_id|car_name|garage_id|---------------------------|1|車1|1|---------------------------|2|車2|1|---------------------------|3|車3|2|---------------------------

                證券表:

                -------------------|security_id|security_name|car_id|----------------------------------|1|安全 1|1|----------------------------------|2|安全2|1|----------------------------------|3|安全3|2|----------------------------------|4|安全 4|3|----------------------------------

                輸出必須是:

                安全 1 在車庫 1保安 2 在車庫 1保安 3 在車庫 1保安 4 在車庫 2

                我有模型:

                代碼是列出車庫,但我想做類似但列出證券(只是為了讓您了解結(jié)構(gòu)如何).

                $garages = Garages::with(['cars', 'securities'])->get();$garages->transform(function($garages) {返回?cái)?shù)組('車庫名稱' =>$garage->garage_name,'count_cars' =>$garage->cars->count(),'count_securities' =>$garage->securities->count(),);});車庫類擴(kuò)展了 Eloquent{公共功能汽車(){返回 $this->hasMany('cars');}公共功能證券(){返回 $this->hasMany('證券');}}類 Cars 擴(kuò)展了 Eloquent{}證券類擴(kuò)展 Eloquent{}

                再次強(qiáng)調(diào):我想知道與保安人員保持安全的汽車相關(guān)的車庫名稱.

                只是為了讓它更容易理解,如果我這樣做:

                $securities = Securities::with(['cars'])->get();證券類擴(kuò)展 Eloquent{公共功能汽車(){返回 $this->hasOne('cars');}}

                我將僅從 cars 表中獲取 garage_id 作為關(guān)系.我真正想要的是車庫的名字.

                [relations:protected] =>大批([汽車] =>汽車對象(...[屬性:受保護(hù)] =>大批(...[汽車名稱] =>車1[車庫 ID] =>1...

                解決方案

                您似乎沒有正確關(guān)聯(lián)對象.讓我們分解一下:

                如果一個(gè)Garage有很多Car,那么一個(gè)Car屬于Garage,讓我們繼續(xù)這個(gè)想法心.

                • 車庫有很多汽車
                • Car有很多Security
                • 安全屬于汽車
                • Garage 通過Car
                • 有很多Security

                在 Eloquent 中,您可以直接將這些關(guān)系添加進(jìn)來,鑒于您在上面發(fā)布的架構(gòu),它應(yīng)該可以工作.

                class Garage 擴(kuò)展了 Eloquent{公共功能汽車(){返回 $this->hasMany('cars');}公共功能證券(){返回 $this->hasManyThrough('Security', 'Car');}}類 Car 擴(kuò)展了 Eloquent{公共功能證券(){返回 $this->hasMany('Security');}//在第二次編輯中添加公共功能車庫(){返回 $this->belongsTo('Garage');}}類 Security 擴(kuò)展了 Eloquent{公共函數(shù) car(){返回 $this->belongsTo('Car');}}

                應(yīng)該就是這樣了.

                您可以從任何模型訪問所有這些關(guān)系,只要有一條路徑可以使用這些關(guān)系的組合從一個(gè)模型繪制到另一個(gè)模型.看看這個(gè),例如:

                $security = Security::with('car.garage')->first();

                將檢索第一個(gè) Security 記錄并在其上加載 Car 關(guān)系,然后再執(zhí)行一步并在每個(gè) Garage 關(guān)系上加載所有 Garage 關(guān)系code>Car 對象加載在 Security 模型下.

                您可以使用以下語法訪問它們:

                $security->car->garage->id//其他例子$garage = Garage::with('cars.securities')->first();foreach($garage->cars as $car){foreach($cars-> 證券作為 $security){echo "汽車 {$car->id} 已分配給它 {$security->id}";}}

                此外,請注意關(guān)系對象是 Collection 的實(shí)例,因此您擁有所有花哨的方法,例如 ->each()、->;count(), ->map() 可用.

                I have three tables garages, cars, securities.

                The securities are the ones that is keeping one car safe, you can have more than one security, but a single security can keep only one car safe. The garage is where the car is and the securities are as well.

                What I want is to list all the securities and know the name of the garage that he is. The problem is that securities doesn't have a column containing the id of the garage, only the id of the car that he is keeping safe, but car has the id of the garage.

                Laravel Eloquent has a method called hasManyThrough, but securities has one garage through cars only.

                Here is the tables:

                garages table:

                -----------------------------------
                |garage_id|garage_name|garage_size|
                -----------------------------------
                |        1|   Garage 1|        200|
                -----------------------------------
                |        2|   Garage 2|        400|
                -----------------------------------
                

                cars table:

                ---------------------------
                |car_id|car_name|garage_id|
                ---------------------------
                |     1|   Car 1|        1|
                ---------------------------
                |     2|   Car 2|        1|
                ---------------------------
                |     3|   Car 3|        2|
                ---------------------------
                

                securities table:

                ----------------------------------
                |security_id|security_name|car_id|
                ----------------------------------
                |          1|  Security 1|      1|
                ----------------------------------
                |          2|  Security 2|      1|
                ----------------------------------
                |          3|  Security 3|      2|
                ----------------------------------
                |          4|  Security 4|      3|
                ----------------------------------
                

                The output must to be:

                Security 1 is on Garage 1
                Security 2 is on Garage 1
                Security 3 is on Garage 1
                Security 4 is on Garage 2
                

                And I have the models:

                The code is to list the garages, but I want to make similar but to list the securities (just for you to have an idea of how the structure is).

                $garages = Garages::with(['cars', 'securities'])->get();
                
                $garages->transform(function($garages) {
                    return array(
                        'garage_name'      => $garage->garage_name,
                        'count_cars'       => $garage->cars->count(),
                        'count_securities' => $garage->securities->count(),
                   );
                });
                
                class Garages extends Eloquent
                {
                    public function cars()
                    {
                        return $this->hasMany('cars');
                    }
                
                    public function securities()
                    {
                        return $this->hasMany('securities');
                    }
                }
                
                class Cars extends Eloquent
                {
                }
                
                class Securities extends Eloquent
                {
                }
                

                And just to make an emphasis again: I want to have the name of the garage related to the car that the security is keeping safe.

                Just to make it even easier to understand, if I do this:

                $securities = Securities::with(['cars'])->get();
                
                class Securities extends Eloquent
                {
                    public function cars()
                    {
                        return $this->hasOne('cars');
                    }
                }
                

                I will get only the garage_id from cars table as relations. What I really want is the name of the garage.

                [relations:protected] => Array
                    (
                        [cars] => Cars Object
                            (
                                ...
                                [attributes:protected] => Array
                                    (
                                        ...
                                        [car_name] => Car 1
                                        [garage_id] => 1
                                        ...
                

                解決方案

                It looks like you are not relating objects correctly. Let's break that down:

                If a Garage Has Many Car then a Car Belongs To Garage, Lets proceed with this idea in mind.

                • Garage Has Many Car
                • Car Has Many Security
                • Security Belongs To Car
                • Garage Has Many Security Through Car

                In Eloquent you can just go ahead and clap those relations in, it should work given the schema you posted above.

                class Garage extends Eloquent
                {
                    public function cars()
                    {
                        return $this->hasMany('cars');
                    }
                
                    public function securities()
                    {
                        return $this->hasManyThrough('Security', 'Car');
                    }
                }
                
                class Car extends Eloquent
                {
                    public function securities()
                    {
                        return $this->hasMany('Security');
                    }
                
                    // ADDED IN SECOND EDIT
                
                    public function garage()
                    {
                        return $this->belongsTo('Garage');
                    }       
                }
                
                class Security extends Eloquent
                {
                    public function car()
                    {
                        return $this->belongsTo('Car');
                    }
                }
                

                And that should be it.

                EDIT: You can access all these relations from any model as long as there is a path you can draw from one model to another using a combination of these relations. Check this out for example:

                $security = Security::with('car.garage')->first();
                

                will retrieve first Security record and load Car relation on it, then it goes one more step and load all Garage relations on every Car object loaded under Security model.

                You can access them using this syntax:

                $security->car->garage->id
                
                // Other examples
                $garage = Garage::with('cars.securities')->first();
                
                foreach($garage->cars as $car)
                {
                    foreach($cars->securities as $security)
                    {
                        echo "Car {$car->id} has {$security->id} assigned to it";
                    }
                }
                

                Furthermore, notice that the relationship objects are an instance of Collection so you have all the fancy methods such as ->each(), ->count(), ->map() available on them.

                這篇關(guān)于有一個(gè)通過 Laravel Eloquent的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                相關(guān)文檔推薦

                Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動(dòng)游標(biāo)不起作用)
                PHP PDO ODBC connection(PHP PDO ODBC 連接)
                Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術(shù)方法)
                php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個(gè)值;等于變量的值)
                MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動(dòng)程序)
                  <tbody id='8sLiH'></tbody>
                • <i id='8sLiH'><tr id='8sLiH'><dt id='8sLiH'><q id='8sLiH'><span id='8sLiH'><b id='8sLiH'><form id='8sLiH'><ins id='8sLiH'></ins><ul id='8sLiH'></ul><sub id='8sLiH'></sub></form><legend id='8sLiH'></legend><bdo id='8sLiH'><pre id='8sLiH'><center id='8sLiH'></center></pre></bdo></b><th id='8sLiH'></th></span></q></dt></tr></i><div class="j2ctdfz" id='8sLiH'><tfoot id='8sLiH'></tfoot><dl id='8sLiH'><fieldset id='8sLiH'></fieldset></dl></div>
                  <legend id='8sLiH'><style id='8sLiH'><dir id='8sLiH'><q id='8sLiH'></q></dir></style></legend>

                  1. <small id='8sLiH'></small><noframes id='8sLiH'>

                      <tfoot id='8sLiH'></tfoot>

                        • <bdo id='8sLiH'></bdo><ul id='8sLiH'></ul>

                          主站蜘蛛池模板: 深圳宣传片制作-企业宣传视频制作-产品视频拍摄-产品动画制作-短视频拍摄制作公司 | 仓储笼_仓储货架_南京货架_仓储货架厂家_南京货架价格低-南京一品仓储设备制造公司 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 | 防水套管_柔性防水套管_刚性防水套管-巩义市润达管道设备制造有限公司 | 淬火设备-钎焊机-熔炼炉-中频炉-锻造炉-感应加热电源-退火机-热处理设备-优造节能 | 生物制药洁净车间-GMP车间净化工程-食品净化厂房-杭州波涛净化设备工程有限公司 | 密集柜_档案密集柜_智能密集架_密集柜厂家_密集架价格-智英伟业 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 全自动实验室洗瓶机,移液管|培养皿|进样瓶清洗机,清洗剂-广州摩特伟希尔机械设备有限责任公司 | 尾轮组_头轮组_矿用刮板_厢式刮板机_铸石刮板机厂家-双驰机械 | TPM咨询,精益生产管理,5S,6S现场管理培训_华谋咨询公司 | 承插管件_不锈钢承插管件_锻钢高压管件-温州科正阀门管件有限公司 | 网带通过式抛丸机,,网带式打砂机,吊钩式,抛丸机,中山抛丸机生产厂家,江门抛丸机,佛山吊钩式,东莞抛丸机,中山市泰达自动化设备有限公司 | 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 中天寰创-内蒙古钢结构厂家|门式刚架|钢结构桁架|钢结构框架|包头钢结构煤棚 | 铝合金风口-玻璃钢轴流风机-玻璃钢屋顶风机-德州东润空调设备有限公司 | 北京公司注册_代理记账_代办商标注册工商执照-企力宝 | 锂电池砂磨机|石墨烯砂磨机|碳纳米管砂磨机-常州市奥能达机械设备有限公司 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-沼河浸过滤器 | 蒜肠网-动漫,二次元,COSPLAY,漫展以及收藏型模型,手办,玩具的新媒体.(原变形金刚变迷TF圈) | LED显示屏_LED屏方案设计精准报价专业安装丨四川诺显科技 | 矿用履带式平板车|探水钻机|气动架柱式钻机|架柱式液压回转钻机|履带式钻机-启睿探水钻机厂家 | 2025世界机器人大会_IC China_半导体展_集成电路博览会_智能制造展览网 | 广东风淋室_广东风淋室厂家_广东风淋室价格_广州开源_传递窗_FFU-广州开源净化科技有限公司 | 杰福伦_磁致伸缩位移传感器_线性位移传感器-意大利GEFRAN杰福伦-河南赉威液压科技有限公司 | 【官网】博莱特空压机,永磁变频空压机,螺杆空压机-欧能优 | 精密光学实验平台-红外粉末压片机模具-天津博君 | WTB5光栅尺-JIE WILL磁栅尺-B60数显表-常州中崴机电科技有限公司 | 山东钢衬塑罐_管道_反应釜厂家-淄博富邦滚塑防腐设备科技有限公司 | 新密高铝耐火砖,轻质保温砖价格,浇注料厂家直销-郑州荣盛窑炉耐火材料有限公司 | 南京展台搭建-南京展会设计-南京展览设计公司-南京展厅展示设计-南京汇雅展览工程有限公司 | 自动气象站_气象站监测设备_全自动气象站设备_雨量监测站-山东风途物联网 | 安徽泰科检测科技有限公司【官方网站】 | 垃圾处理设备_餐厨垃圾处理设备_厨余垃圾处理设备_果蔬垃圾处理设备-深圳市三盛环保科技有限公司 | ph计,实验室ph计,台式ph计,实验室酸度计,台式酸度计 | 打包钢带,铁皮打包带,烤蓝打包带-高密市金和金属制品厂 | 立式硫化罐-劳保用品硫化罐-厂家直销-山东鑫泰鑫硫化罐厂家 | 山东钢衬塑罐_管道_反应釜厂家-淄博富邦滚塑防腐设备科技有限公司 | 橡胶接头|可曲挠橡胶接头|橡胶软接头安装使用教程-上海松夏官方网站 | 天津仓储物流-天津电商云仓-天津云仓一件代发-博程云仓官网 |