我有用户和配置文件模型与追随者/以下。我想要的是获得用户数据与配置文件数据合并时,调用配置文件::追随者。现在我只检索概要文件数据。
所以我将user()添加到Profile.php中,这样我就可以调用Profile::follower ->user()…但是没有结果。有人能解释如何合并用户与配置文件时,调用following()
函数?
<User.php模型/strong>
public function following()
{
return $this->belongsToMany(Profile::class);
}
public function profile()
{
return $this->hasOne(Profile::class);
}
<Profile.php模型/strong>
public function followers()
{
return $this->belongsToMany(User::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
我不知道我是否迟到了,但是获得用户资料的正确方法是
$profile = Auth::user()->profile;
我不知道这是否有帮助,使用Auth::user()
你可以获取当前用户,因为你与配置文件有关系,你可以调用profile()
公共函数而不带大括号。
我希望这对任何人都有帮助。