here is my app.blade
$('.like').click(function(event) {
var id = $(this).data('id');
$.ajax({
url: "{{route('post.like','')}}/"+id,
})
});
and here is my AdsController.php
public function likePost(Request $request){
$likePost=new Like;
$likePost->user_id=Auth::user()->id;
$likePost->ad_id= $request->id;
$likePost->save();
}
when I click on like button the data insert in the likes table but I want to return the number of the likes in the view also
In your
likePostfunction, after you've saved theLikethen do one of the following:If you want to return the number of likes the user has:
If you want to return the number of likes the ad has:
If you want to return the number of all likes:
Then, as it is an AJAX request, you will need to return a JSON response in your
likePostfunction: https://laravel.com/docs/5.6/responses#json-responsesAnd in your JavaScript, pick up the response like this:
-- edited --
To show in the view, simply add a class or an id to the html element you want to display the number, for example:
<div id="numberOfLikes"></div>You can then set it's inner html content using the response you got from AJAX: