Deekshith's avatar

Laravel combine followers and following users

i have a relationship like below, User.php

public function followers()
    {
        return $this->belongsToMany('App\User', 'followers', 'leader_id', 'follower_id')->withPivot('status');
    }

    public function following()
    {
        return $this->belongsToMany('App\User', 'followers', 'follower_id', 'leader_id');
    }

now in controller i can fetch followers and following like below,

$user = auth()->user();
$following_users = $user->following()->where('status', '=', 'approved')->get();
$followers_users = $user->followers()->where('status', '=', 'approved')->get();

Now i am trying to fetch users combing followers and following in one query and apply pagination for that.

example:

if followers has below data

name1, name2, name3

if following has below data,

followingname1, followingname2, followingname3

Now i want to write a query to print data combining followers and following like below,

name1, name2, name3,followingname1, followingname2, followingname3

Any solution? Thank you.

0 likes
1 reply

Please or to participate in this conversation.