Skip to content

Instantly share code, notes, and snippets.

@dopsmain
Forked from mydnic/button.blade.php
Created June 23, 2016 12:52
Show Gist options
  • Save dopsmain/baaf65b07cf2b7ae03483f2a03e4ce08 to your computer and use it in GitHub Desktop.
Save dopsmain/baaf65b07cf2b7ae03483f2a03e4ce08 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment