0

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.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
ExohJosh
  • 1,832
  • 16
  • 22
  • 2
    Possible duplicate of [Friendship system with Laravel : Many to Many relationship](http://stackoverflow.com/questions/25049753/friendship-system-with-laravel-many-to-many-relationship) – Fabio Antunes Mar 08 '16 at 15:15

1 Answers1

0

Fixed it by fixing my relationships as per the suggested duplicate Fabio Antunes posted (Friendship system with Laravel : Many to Many relationship)

Community
  • 1
  • 1
ExohJosh
  • 1,832
  • 16
  • 22