title: Make an asynchronous post request laravel to handle likes?
body: <p>Currently I am handling likes like so:</p>
  <pre><code>    class LikeController extends Controller
      {
          public function like(Post $post)
          {
  
              $attributes = [
                  ['user_id', '=', auth()-&gt;user()-&gt;id],
                  ['post_id', '=', $post-&gt;id]
              ];
  
              $like = Like::where($attributes);
  
              if($like-&gt;exists()) {
                  $like-&gt;delete();
  
              } else {
                  Like::create(['user_id' =&gt; auth()-&gt;user()-&gt;id, 'post_id' =&gt; $post-&gt;id]);
              }
  
              return redirect()-&gt;to(url()-&gt;previous());
  
          }
      }
  </code></pre>
  <p>and I have a route:</p>
  <pre><code>    Route::post('like/{post}', [LikeController::class, 'like']);
  </code></pre>
  <p>What I don't like is that each time a like is clicked a new request is sent and the page refreshes to the top, so I added this javascript to scroll back to the previous position when a like is clicked:</p>
  <pre><code>    $(window).on(&quot;scroll&quot;, function(e) {
          $.cookie(&quot;tempScrollTop&quot;, $(window).scrollTop());
      });
      
      $(&quot;.like-form&quot;).each(function() {
          $(this).submit(function(e) {
              $.cookie(&quot;action&quot;, 'like', {path: '/'});
          })
      });
      
      $(window).on(&quot;load&quot;, function (e) {
          if ($.cookie(&quot;tempScrollTop&quot;) &amp;&amp; $.cookie('action') === 'like') {
              $(window).scrollTop($.cookie(&quot;tempScrollTop&quot;));
              $.cookie(&quot;action&quot;, '', {path: '/'});
          }
      })
  
  
  </code></pre>
  <p>This all works great, but I still don't like seeing the page refresh when hitting a like button. Is there a way to make the post request async and/or let it run in the background such that when hitting the like button the page doesn't refresh but the like does get sent to the server? What would be otherwise the correct method to achieve this result?</p>
  <p>Thank you any help will be appreciated!</p>