php - 如何从mysql中的多个表关系中检索数据

标签 php mysql laravel laravel-5.5

我正在使用 php Laravel 5.5 开发一个项目。我有表 tbl_borrower、tbl_property_owner、tbl_property、tbl_property_borrower。表的结构是这样的。 tbl_property_owner has one or many properties and property are assigned to the borrower in tbl_property_borrower. The borrower can also have many properties. 在报告中,我必须显示借款人详细信息、属性(property)所有者详细信息、分配的属性(property)详细信息。报告应该是这样的。 In report, there should be borrower details and property details and assigned property details are shown on the basis of tbl_property_borrower 我试过在这样的模型中建立一对多关系。

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/

相关文章:

php - 数组键作为用于插入的 mysql 列名?

php - mysql查询获取特定日期之间用户的详细信息

php - 创建正则表达式以匹配 00 :00:00 for duration (not time) 的格式

php - 函数 edit() 的语法文件夹存储库

php - 将多个数据分组

php - 按字母顺序打印数据库结果

MySQL WHERE 和类似问题

php - Laravel 几分钟后自动注销

javascript - Jasny 多文件上传与其他表单元素

mysql - 从 Sequelize include 中的 Join Table 获取 key