Reputation:
I'm trying to allow a user to follow a user, but i don't want the user to follow itself.
I'm using laravel follow, which allows you to follow a user. However, i can follow myself easily and i don't want that. I want it set where you wont be able to follow yourself, this is what i have so far.
Profile.blade.php
<!-- maybe there should be a blade function that can hide the following below so a user wont be able to follow itself. -->
<div>
<i ng-click="myfollow({{$user}});" class="glyphicon glyphicon-plus">Follow</i>
</div>
UserController.php
public function my_follow(Request $request, $id)
{
$user = auth()->user();
if(User::find($id)){
$user->toggleFollow(User::find($id));
}
}
Main.js
$scope.myfollow = function(user) {
$http.post('/user/follow/'+ user.id).then(function(result) {
console.log("user id is:"+ user.id);
});
};
Upvotes: 0
Views: 111
Reputation: 24383
You can get the id
of the current user, by doing auth()->user()->id
. So just add a check to make sure it's not the same as the ID you're trying to follow:
public function my_follow(Request $request, $id)
{
$user = auth()->user();
if($user->id != $id && $otherUser = User::find($id)){
$user->toggleFollow($otherUser);
}
}
Upvotes: 3