php 在 Laravel 中使用 Eloquent 检索关系的关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18963483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving relationships of relationships using Eloquent in Laravel
提问by Ben Thompson
I have a database with the following tables and relationships:
我有一个包含以下表和关系的数据库:
Advert 1-1
Car m-1
Model m-1
Brand
广告1-1
车m-1
型号m-1
品牌
If I want to retrieve an Advert, I can simply use:
如果我想检索广告,我可以简单地使用:
Advert::find(1);
If I want the details of the car, I could use:
如果我想要汽车的详细信息,我可以使用:
Advert::find(1)->with('Car');
However, if I also want the detail of the Model (following the relationship with Car), what would the syntax be, the following doesn't work:
但是,如果我还想要模型的细节(遵循与 Car 的关系),那么语法是什么,以下不起作用:
Advert::find(1)->with('Car')->with('Model');
Many thanks
非常感谢
回答by Bj?rn
It's in the official documentationunder "Eager Loading"
它在“Eager Loading”下的官方文档中
Multiple relationships:
多重关系:
$books = Book::with('author', 'publisher')->get();
Nested relationships:
嵌套关系:
$books = Book::with('author.contacts')->get();
So for you:
所以对你来说:
Advert::find(1)->with('Car.Model')->get();
回答by Antonio Carlos Ribeiro
First you need to create your relations,
首先你需要建立你的关系,
<?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');
}
}
Then you just have to access this way:
然后你只需要通过这种方式访问:
echo Advert::find(1)->car->model->brand->name;
But your table fields shoud be, because Laravel guess them that way:
但是你的表字段应该是,因为 Laravel 是这样猜测的:
id (for all tables)
car_id
model_id
brand_id
Or you'll have to specify them in the relationship.
或者您必须在关系中指定它们。
回答by Harsh Gehlot
Suppose you have 3 models region,city,hotels and to get all hotels with city and region then
假设您有 3 个模型 region,city,hotels 并获得带有城市和地区的所有酒店
Define relationship in them as follows:-
定义它们之间的关系如下:-
Hotel.php
酒店.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
城市.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
区域.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
酒店控制器.php
public function getAllHotels(){
// get all hotes with city and region
$hotels = Hotel::with('city.regions')->get()->toArray();
}