首页 > 解决方案 > Laravel 通过配置文件获取用户数据

问题描述

我有带有关注者/关注者的用户和个人资料模型。我想要的是在调用 Profile::followers 时将用户数据与 Profile 数据合并。现在我只检索配置文件数据。

所以我将 user() 添加到 Profile.php 中,这样我就可以调用 Profile::followers->user()... 但没有结果。following()有人可以解释在调用函数时如何将用户与个人资料合并吗?

User.php 模型

public function following()
{
   return $this->belongsToMany(Profile::class);
}

public function profile()
{
    return $this->hasOne(Profile::class);
}

Profile.php 模型

public function followers()
{
    return $this->belongsToMany(User::class);
}

public function user()
{
    return $this->belongsTo(User::class);
}

标签: phplaravelmerge

解决方案


推荐阅读