如果用户正在关注某个页面,我正在尝试获取,但返回空...
在关系中返回空:
public function get_following()
{
return $this->belongsTo(Followers::class, 'user_id')->where('type', '=', 0)->where('follower_id', '=', auth()->user()->id);
}
DD($用户);
UserProfile {#301 ▼
#table: "users_profile"
#relations: array:3 [▼
"user" => User {#305 ▶}
"shares" => Collection {#307 ▶}
"get_following" => null
]
此代码返回结果:
$get_follow = Followers::where('type', '=', 0)->where('follower_id', '=', auth()->user()->id)->where('user_id', '=', 1)->first();
dd($get_follow);
返回:
Followers {#329 ▼
#attributes: array:4 [▼
"id" => 22
"user_id" => 1
"follower_id" => 1
"type" => 0
]
首先,尝试更好地命名关系,它可以帮助您更好地理解主题
class UserProfile extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function followers()
{
return $this->user()->followers();
}
}
class User extends Model
{
Public function followers()
{
return $this hasMany(Follower::class);
}
}
Class Follower extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
现在,您如何从个人资料中获得关注者?
$profile->followers