I have my Relation table which goes as following:
UserId (PK)
FollowingId
created_at
updated_at
I am able to get who the user is following and their usernames by doing the following:
My controller:
$findingWhoIFollow = User::find($userId)->relations()->FindUserName()->get();
My model (user):
public function relations()
{
return $this->hasMany(Relation::class, 'UserId', 'id');
}
My model: (relation)
protected $primaryKey = 'UserId';
public function user()
{
return $this->belongsToMany(User::class, 'id', 'UserId');
}
public function scopeFindUserName($query)
{
return $query->join('User', 'User.id', '=', 'Relation.FollowingId');
}
and then i am able to get the usernames of the people I follow like this:
@foreach($findingWhoIFollow as $iFollow)
{{$iFollow->Username}}
@endforeach
I am however struggling to do the inverse of this eg: getting the users who follow my accounts user names. I believe it's an issue with my relationships and I am missing something obvious.