Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Laravel中的Elount检索关系中的关系_Php_Laravel_Eloquent_Object Relationships - Fatal编程技术网

Php 使用Laravel中的Elount检索关系中的关系

Php 使用Laravel中的Elount检索关系中的关系,php,laravel,eloquent,object-relationships,Php,Laravel,Eloquent,Object Relationships,我有一个包含以下表和关系的数据库: $books = Book::with('author', 'publisher')->get(); $books = Book::with('author.contacts')->get(); 广告1-1Carm-1Modelm-1Brand 如果我想检索广告,我可以简单地使用: Advert::find(1); 如果我想了解汽车的详细信息,我可以使用: Advert::find(1)->with('Car'); 但是,如果我还需要

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

$books = Book::with('author', 'publisher')->get();
$books = Book::with('author.contacts')->get();
广告
1-1
Car
m-1
Model
m-1
Brand

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

Advert::find(1);
如果我想了解汽车的详细信息,我可以使用:

Advert::find(1)->with('Car');
但是,如果我还需要模型的详细信息(遵循与Car的关系),那么语法是什么,以下内容不起作用:

Advert::find(1)->with('Car')->with('Model');
非常感谢

它在官方的“急切加载”下

多重关系:

$books = Book::with('author', 'publisher')->get();
$books = Book::with('author.contacts')->get();
嵌套关系:

$books = Book::with('author', 'publisher')->get();
$books = Book::with('author.contacts')->get();
所以对你来说:

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

首先你需要建立你的关系

<?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');
    }

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

id (for all tables)
car_id
model_id
brand_id

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

假设您有3个模型区域、城市、酒店,然后获得所有具有城市和区域的酒店

定义其中的关系,如下所示:-

php酒店

class Hotel extends Model {

  public function cities(){
        return $this->hasMany(City::class);
  }

  public function city(){
        return $this->belongsTo('App\City','city_id');
  }
}
City.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();

}

啊,太棒了+1参考急切加载-我已经通过了雄辩的部分,但没有找到我的问题的答案!这是否也适用于“hasMany”关系?它仅适用于我的“hasOne”/“belongsTo”关系。文档链接现在是:在“嵌套的即时加载”下
Advert::find(1)->with('Car.Model')->get()
将实际返回laravel 8中的所有广告记录<代码>广告::with('Car.Model')->查找(1)返回特定的代码