久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    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="qwawimqqmiuu" 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>

      有一個通過 Laravel Eloquent

      Has one through Laravel Eloquent(有一個通過 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="qwawimqqmiuu" id='fTCZp'><tfoot id='fTCZp'></tfoot><dl id='fTCZp'><fieldset id='fTCZp'></fieldset></dl></div>

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

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

                問題描述

                我有三個表garagescarssecurities.

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

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

                Laravel Eloquent 有一個名為 hasManyThrough 的方法,但 securities 只有一個 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) {返回數(shù)組('車庫名稱' =>$garage->garage_name,'count_cars' =>$garage->cars->count(),'count_securities' =>$garage->securities->count(),);});車庫類擴展了 Eloquent{公共功能汽車(){返回 $this->hasMany('cars');}公共功能證券(){返回 $this->hasMany('證券');}}類 Cars 擴展了 Eloquent{}證券類擴展 Eloquent{}

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

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

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

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

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

                解決方案

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

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

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

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

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

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

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

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

                將檢索第一個 Security 記錄并在其上加載 Car 關(guān)系,然后再執(zhí)行一步并在每個 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 的實例,因此您擁有所有花哨的方法,例如 ->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)于有一個通過 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 可滾動游標不起作用)
                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 獲取一個值;等于變量的值)
                MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動程序)
                  <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="qwawimqqmiuu" 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>

                          主站蜘蛛池模板: 国产一区在线免费观看 | 成人在线电影在线观看 | 久久久久国产一区二区三区 | 日韩一区在线视频 | 日韩中文字幕网 | 国产精品视频在线播放 | 国产在线精品一区二区 | 午夜视频一区二区三区 | 亚洲一区二区三区免费在线观看 | 伊人伊人伊人 | 不卡一区二区三区四区 | 韩国主播午夜大尺度福利 | 99精品国产一区二区三区 | 9久9久9久女女女九九九一九 | 久久一区二区视频 | 日本不卡视频在线播放 | 尤物在线 | 日韩中文字幕一区二区 | 韩日三级 | 久久久精品久久 | 欧美日韩欧美 | 日韩欧美一区二区三区免费观看 | av在线免费观看网站 | 成人在线精品视频 | 99热国产在线播放 | 亚洲精品在线视频 | 亚洲久草视频 | 亚洲成人激情在线观看 | 亚洲欧美高清 | 日韩精品一区二区三区中文在线 | 91在线看| 日屁视频 | 伊人伊人伊人 | 久久亚洲一区 | 精品久久成人 | 国内精品久久影院 | 久久久国产一区 | 国产成人短视频在线观看 | 国产精品18久久久 | 国产亚洲精品久久久久动 | 欧美日韩国产精品激情在线播放 |