I have created this button:
<button onclick="addToFavourites({{ $product->prd_id }})" class="btn btn-primary">
<i class="fa fa-heart"></i>
<span>
Add to favourites
</span>
</button>
So as you can see I have added an onlick function named addToFavourites
which goes here:
function addToFavourites(productId) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: baseurl + "/add-to-favourites",
type: "POST",
data: {
prd_id: parseInt(productId)
},
dataType: "json",
success: function (output) {
$('#afterAddToFavourites').modal('show');
}
});
}
So this function will call the route with URI of add-to-favourites
with POST method:
Route::post("add-to-favourites", "ShopCartsController@addToFavourites")->name('addToFavourites');
And in the CartsController:
public function addToFavourites()
{
$favPros = new FavouriteProduct();
$favPros->user_id = auth()->user()->user_id;
$favPros->prd_id = request()->prd_id;
$favPros->save();
}
But now the problem is whenever I click on the button, it does not do anything at all!
And at the Console, I see this error:
POST https://sitename.com//add-to-favourites 500 (Internal Server Error)
send @ jquery.js:2
ajax @ jquery.js:2
addToFavourites @ProductName:1187
onclick @ProductName:458
But I don’t what’s going wrong here and how can I fix this…
So if you know, please help me out cause I really need this…
Thanks in advance.