title: Like System with AJAX and Laravel
body: <p>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?</p>
  
  <p>Here is what i have so far:</p>
  
  <pre><code>Table: 
  likes: id, post_id, user_id, timestamps()
  </code></pre>
  
  <p>HTML:</p>
  
  <pre><code>&lt;a class="btn btn-not-liked like"&gt;Liked?&lt;/a&gt;
  </code></pre>
  
  <p>AJAX JS:</p>
  
  <pre><code>var postId = 0;
  var token = '{{ Session::token() }}';
  var urlLiKes = '{{ route('likes') }}';
  $('.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() {
  
       });
  
       });
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>$post_id = $request['postId'];
          $is_visit = $request['isLike'] === 'true';
          $post = Post::find($post_id);
          if (!$post) {
              return null;
          }
          $liked = Auth::user()-&gt;likes()-&gt;where('post_id', $post_id)-&gt;first();
          if($liked == $is_like){
              $liked-&gt;delete();
              return null;
          }
          else{
              $liked = new Like();
          }
          $liked-&gt;user_id = Auth::user();
          $liked-&gt;post_id = $post-&gt;id;
          $liked-&gt;save();
          return null;
  }
  </code></pre>
  
  <p>Please help to find the issue in 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.</p>