dimanche 3 novembre 2019

like and unlike with javascript and Laravel

I'm trying to implement a functionality where user can like and unlike product using javascript I followed this Laravel - Favourite / Un-Favourite button but it doesn't work for me, the button can't be clicked, any idea on how I can fix this?

Routes

Route::get('product/like/{id}', ['as' => 'product.like', 'uses' =>   'LikeController@likeProduct']);
Route::get('product/{product}/unlike','LikeController@destroy')->name('product.unlike');

Javascript

 <script>

function addToFavourites(productid, userid) {
var user_id = userid;
var product_id = productid;

$.ajax({
    type: 'post',
    url: 'product/like/{id}',
    data: {
        'user_id': user_id,
        'product_id': product_id,
    },
    success: function () {
        // hide add button
        $('#addfavourites' + product_id).hide();
        // show delete button
        $('#deletefavourite' + product_id).show();
    },
    error: function (XMLHttpRequest) {
        // handle error
    }
});
}

Blade file

   @if($product->isLiked)
      <div id="addfavourites" onClick="addToFavourites("> unlike </div>
       @else
       <div id="deletefavourite" onClick="deleteFromFavourites(" > like </div>
    @endif


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire