【问题标题】:Retrieving relationships of relationships using Eloquent in Laravel在 Laravel 中使用 Eloquent 检索关系的关系
【发布时间】:2013-09-28 14:22:51
【问题描述】:

我有一个包含以下表格和关系的数据库:

广告1-1汽车m-1车型m-1品牌

如果我想检索广告,我可以简单地使用:

Advert::find(1);

如果我想要汽车的详细信息,我可以使用:

Advert::find(1)->with('Car');

但是,如果我还想要模型的详细信息(遵循与 Car 的关系),语法是什么,以下不起作用:

Advert::find(1)->with('Car')->with('Model');

非常感谢

【问题讨论】:

    标签: php laravel eloquent object-relationships


    【解决方案1】:

    在官方documentation“Eager Loading”下

    多重关系:

    $books = Book::with('author', 'publisher')->get();
    

    嵌套关系:

    $books = Book::with('author.contacts')->get();
    

    所以对你来说:

    Advert::with('Car.Model')->find(1);
    

    【讨论】:

    • 啊,太好了! +1 参考 Eager Loading - 我已经通过 Eloquent 部分但没有找到我的问题的答案!
    • 这也适用于“hasMany”关系吗?它仅适用于我的“hasOne”/“belongsTo”关系。
    • 文档链接现在是:laravel.com/docs/5.5/eloquent-relationships#eager-loading under "Nested Eager Loading"
    • Advert::find(1)->with('Car.Model')->get(); 实际上会返回 laravel 8 中的所有广告记录。Advert::with('Car.Model')->find(1); 返回具体的记录
    【解决方案2】:

    首先你需要创建你的关系,

    <?php
    
    class Advert extends Eloquent {
    
        public function car()
        {
            return $this->belongsTo('Car');
        }
    
    }
    
    class Car extends Eloquent {
    
        public function model()
        {
            return $this->belongsTo('Model');
        }
    
    }
    
    class Model extends Eloquent {
    
        public function brand()
        {
            return $this->belongsTo('Brand');
        }
    
        public function cars()
        {
            return $this->hasMany('Car');
        }
    
    }
    
    class Brand extends Eloquent {
    
        public function models()
        {
            return $this->hasMany('Model');
        }
    
    }
    

    那么你只需要这样访问:

    echo Advert::find(1)->car->model->brand->name;
    

    但是你的表格字段应该是,因为 Laravel 是这样猜测的:

    id (for all tables)
    car_id
    model_id
    brand_id
    

    或者您必须在关系中指定它们。

    【讨论】:

      【解决方案3】:

      假设你有 3 个模型 region,city,hotels 然后得到所有带有城市和地区的酒店

      定义它们之间的关系如下:-

      Hotel.php

      class Hotel extends Model {
      
        public function cities(){
              return $this->hasMany(City::class);
        }
      
        public function city(){
              return $this->belongsTo('App\City','city_id');
        }
      }
      

      城市.php

      class City extends Model {
      
        public function hotels(){
            return $this->hasMany(Hotel::class);
        }
      
        public function regions(){
            return $this->belongsTo('App\Region','region_id');    
        }
      }
      

      Region.php

      class Region extends Model
      {
      
        public function cities(){
            return $this->hasMany('App\City');    
        }
      
        public function country(){
            return $this->belongsTo('App\Country','country_id');
        } 
      }
      

      HotelController.php

      public function getAllHotels(){
          // get all hotes with city and region
          $hotels = Hotel::with('city.regions')->get()->toArray();
      
      }
      

      【讨论】:

        【解决方案4】:

        将添加关系函数只是询问所需的关系

        public function Car()
        {
            return $this->belongsTo(Car::class, 'car_id')->with('Model');
        }
        

        但如果你想要一个嵌套关系,只需使用 with 中的句点

        Advert::with('Car.Model')->find(1);
        

        但是对于多关系使用数组

        Advert::with('Car','Model')->find(1);
        

        【讨论】:

          猜你喜欢
          • 2019-08-24
          • 1970-01-01
          • 1970-01-01
          • 2019-08-17
          • 2019-12-11
          • 2022-07-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多