Код: Выделить всё
$(".add-to-compare").on("click", function(e) {
e.preventDefault();
var uid = $(this).attr("id");
var vin = $(this).attr("data-vin");
var compare_array = Cookies.get('compare');
compare_array = compare_array ? JSON.parse(compare_array) : [];
var has_uid = compare_array.some(function(item) {
return item.uid === uid;
});
if (has_uid) {
show_notification("warning","Item already exists in the compare list.");
} else if (compare_array.length >= 4) {
show_notification("warning","You can only compare up to 4 items.");
} else {
compare_array.push({ uid: uid, vin: vin });
Cookies.set('compare', JSON.stringify(compare_array), { expires: 7, path: '/' });
}
$("#compare-modal").modal("show");
console.log("Geted cookies:", Cookies.get("compare"));
});
$("#compare-modal").on('show.bs.modal', function(e) {
$.ajax({
url: "getCompareItems",
type: "GET",
success: function(data) {
console.log("Compare items from server:", data);
},
});
});
Код: Выделить всё
public function getCompareItems(Request $request)
{
$compare_cookie = $request->cookie('compare');
$compare_array = $compare_cookie ? json_decode($compare_cookie, true) : [];
return response()->json($compare_array);
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... in-laravel