I used many to many relationship in my user table to make the logged in user to follow another one, but I didn't figuerd it out by myself, I checked what others did and tried to do something similar and it worked. In my methods I have:
function follow(User $user) {
$this->followers()->attach($user->id);
}
function unfollow(User $user) {
$this->followers()->detach($user->id);
}
which allows me follow.
the tables are related with a function something like:
return $this->belongsToMany('App\User', 'followers', 'user_id', 'follower_id');
now I pass the $user value by the controller and the controller is pretty simple:
$userId = User::find($user);
$willfollow = Auth::user();
$willfollow->unfollow($userId);
I know the controller information is probably not needed but incase it is easy to check the relation withing the controller I would prefer to do it that way, cuz I'm obviously don't have that much knowledge of metod use.
Laravel 5.4
Thanks for any help.
Aucun commentaire:
Enregistrer un commentaire