【发布时间】:2023-01-04 22:55:19
【问题描述】:
我想显示来自至少有一个“residente”(子表)的“personas”(父表)的数据,它是一对多的关系,我也想显示那个 residente 的数据。 我试图使用 has() 方法来做到这一点,就像 laravel 文档所说的那样: https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-existence 但它不起作用。
模型看起来像这样
//in the Persona class
public function residentes()
{
return $this->hasMany(Residente::class);
}
//in the Residente class
public function persona()
{
return $this->belongsTo(Persona::class);
}
//in the PersonasController
public function index()
{
$personas = Persona::has('residentes')->get();
dd($personas);
}
结果 enter image description here //它没有从“residentes”获取数据
【问题讨论】:
标签: php laravel vue.js eloquent