I'm trying to get it so people can like a post on a button click which will upload a row to the likes table and then if they press the button again it will delete the record and unlike the post. The problem i am having is that i have rehashed a tutorial which included a boolean for a like and dislike, i do not have the boolean and have tried to reconfigure the code to accommodate that. The problem i'm having is that after i've sorted out the unauthorized errors the button now appears to be dead and doesn't do anything. I presume this mean that their is an issue with the laravel code for this. Can anyone see why the button isnt responding and point me in the direction of how i like(post) to database, check for a like and delete if the button is pressed and the row exists?
Here is what i have so far:
Table: likes: id, post_id, user_id, timestamps()
HTML:
<a class="btn btn-not-liked like">Liked?</a>
AJAX JS:
var postId = 0;
var token = '';
var urlLiKes = '';
$('.like').on('click', function(event){
event.preventDefault();
postId = event.target.parentNode.parentNode.dataset['postid'];
var isVisit = event.target.previousElementSibling == null;
$.ajax({
method: 'POST',
url: urlLikes,
data: {isLikes: isLikes, postId: postId, _token: token}
})
.done(function() {
});
});
PHP:
$post_id = $request['postId'];
$is_visit = $request['isLike'] === 'true';
$post = Post::find($post_id);
if (!$post) {
return null;
}
$liked = Auth::user()->likes()->where('post_id', $post_id)->first();
if($liked == $is_like){
$liked->delete();
return null;
}
else{
$liked = new Like();
}
$liked->user_id = Auth::user();
$liked->post_id = $post->id;
$liked->save();
return null;
}
can anyone find the issue for this code or point me in the direction of a like system without the boolean and just a simple like(post)/unlike(delete with Laravel and AJAX.
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2G29MdO
via IFTTT
Aucun commentaire:
Enregistrer un commentaire