Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

You don't need a UserUser model.

class User extends Eloquent{

public function followers(){//they follow this user
return $this->belongsToMany('User', 'user_user', 'id_user', 'id_userseguito');
}

public function followings(){//this user follows them
return $this->belongsToMany('User', 'user_user', 'id_userseguito', 'id_user');
}
}

Check if it works.

$user = User::find($id);

$followerNames = array();
foreach($user->followers as $follower){
$followerNames[] = $follower->username;
}

$followingNames = array();
foreach($user->followings as $following){
$followingNames[] = $following->username;
}

return array(
'user' => $user,
'followers' => $followerNames,
'followings' => $followingNames 
);

De nada :P

Last updated 2 years ago.
0

Thanks!!!!! ;)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.