【发布时间】:2021-07-17 04:04:15
【问题描述】:
我正在尝试使用 Laravel eloquent 从数据库中获取数据,但它没有返回任何数据。这是数据库结构
-
地区
- 身份证
- 姓名
-
区
- 身份证
- 姓名
- region_id
-
病房
- 身份证
- 姓名
- region_id
因此,病房与地区无关,而与地区有关。我怎样才能获得病房(数据)?这就是我获取数据的方式
Area::with('region.district.ward')->get();
型号
Region.php
public function district()
{
return $this->hasMany(District::class);
}
public function ward()
{
return $this->hasMany(Ward::class);
}
区.php
public function region()
{
return $this->belongsTo(Region::class);
}
Ward.php
public function regions()
{
return $this->belongsTo(Region::class);
}
【问题讨论】:
-
可能你想要
Region::with('district')->with('ward')->get();这样的东西 -
如果我使用它,如果我有
Area::with('region.district.ward')->get();我如何获得ward? @EsTeAa -
这样
Area::with('region.ward')->get();或者一起Area::with(['region.district','region.ward'])->get();
标签: laravel laravel-5 eloquent