displaying follow and unfollow buttons accordingly

This commit is contained in:
Ghostie 2024-08-23 15:22:13 -05:00
parent 6ab13ff55a
commit f12ac2489a
2 changed files with 22 additions and 13 deletions

View File

@ -62,5 +62,11 @@ class User extends Authenticatable
return $this->belongsToMany(User::class, "followers", "user_id", "follower_id");
}
// check if a user already follows another user
public function following(User $user)
{
return $this->followers->contains($user->id);
}
// store the following
}

View File

@ -47,19 +47,22 @@
@auth
@if ($user->id !== auth()->user()->id)
<form action="{{ route('users.follow', $user) }}" method="POST">
@csrf
<input type="submit"
class="bg-blue-600 text-white uppercase rounded-lg px-3 py-1 text-xs font-bold cursor-pointer"
value="follow">
</form>
<form action="{{ route('users.unfollow', $user) }}" method="POST">
@csrf
@method('DELETE')
<input type="submit"
class="bg-red-600 text-white uppercase rounded-lg px-3 py-1 text-xs font-bold cursor-pointer"
value="unfollow">
</form>
@if (!$user->following(auth()->user()))
<form action="{{ route('users.follow', $user) }}" method="POST">
@csrf
<input type="submit"
class="bg-blue-600 text-white uppercase rounded-lg px-3 py-1 text-xs font-bold cursor-pointer"
value="follow">
</form>
@else
<form action="{{ route('users.unfollow', $user) }}" method="POST">
@csrf
@method('DELETE')
<input type="submit"
class="bg-red-600 text-white uppercase rounded-lg px-3 py-1 text-xs font-bold cursor-pointer"
value="unfollow">
</form>
@endif
@endif
@endauth
</div>