Skip to content

Instantly share code, notes, and snippets.

@mydnic
Last active October 15, 2019 10:44
Show Gist options
  • Save mydnic/278e485b9e636c491ab1 to your computer and use it in GitHub Desktop.
Save mydnic/278e485b9e636c491ab1 to your computer and use it in GitHub Desktop.
Simple Like System in Laravel 5
<div ng-app="Actions">
<span ng-controller="LikeController">
@if ($post->user->id != Auth::id())
<button class="btn btn-default like btn-login" ng-click="like()">
<i class="fa fa-heart"></i>
<span>@{{ like_btn_text }}</span>
</button>
@endif
</span>
</div>
<script>
var app = angular.module("Actions", []);
app.controller("LikeController", function($scope, $http) {
checkLike();
$scope.like = function() {
var post = {
id: "{{ $post->id }}",
};
$http.post('/api/v1/post/like', post).success(function(result) {
checkLike();
});
};
function checkLike(){
$http.get('/api/v1/post/{{ $post->id }}/islikedbyme').success(function(result) {
if (result == 'true') {
$scope.like_btn_text = "Delete Like";
} else {
$scope.like_btn_text = "Like";
}
});
};
});
</script>
@mydnic
Copy link
Author

mydnic commented Nov 18, 2015

@pbelyaev
Copy link

@mydnic the url doesn't work 😢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment