我正在使用 php Laravel 5.5 开发一个项目。我有表 tbl_borrower、tbl_property_owner、tbl_property、tbl_property_borrower。表的结构是这样的。 在报告中,我必须显示借款人详细信息、属性(property)所有者详细信息、分配的属性(property)详细信息。报告应该是这样的。 我试过在这样的模型中建立一对多关系。
public function properties()
{
return $this->hasMany('App\Property','property_owner_id');
}
public function assigned_property()
{
return $this->hasMany('App\PropertyBorrower','property_id');
}
像这样,我可以检索属性(property)所有者的属性(property)详细信息,但我不能 根据属性(property)所有者获得分配给借款人的值(value)。 提前致谢。
最佳答案
像下面这样尝试
use App\Property;
use App\PropertyBorrower;
public function properties()
{
return $this->hasMany(Property::class, 'property_owner_id', 'id');
}
public function assigned_property()
{
return $this->hasMany(PropertyBorrower::class, 'property_id', 'id');
}
在您的借款人模型中
public function borrower()
{
return $this->belongsTo(PropertyBorrower::class, 'id', 'borrower_id');
}
关于php - 如何从mysql中的多个表关系中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53629869/