I have a problem when i open my website i have a error like: Undefined index: cart_id but when i reload page it works i dont know whats problem. In my controller I defined that cookie and in my view to. In my browser brave it works (i dont need to reload page like in other browsers) i dont know whats wrong, why it works only when i reload page?
@php
if(!isset($_COOKIE['cart_id'])) setcookie('cart_id',uniqid());
@endphp
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" href="{{env('APP_URL')}}/public/images/icon.jpg">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet" href="{{env('APP_URL')."/public/css/style.min.css"}}">
<title>@yield("title")</title>
</head>
<body>
@php
$items = \Cart::session($_COOKIE['cart_id'])->getContent();
$price = 0;
foreach ($items as $item)
{
$price = $price + $item->price * $item->quantity;
}
@endphp
<header class="header">
<div class="container">
<div class="burger__background burger__item">
<img class = "close__button" src="{{env('APP_URL')}}/public/images/icons/close.svg" alt="">
</div>
<div class="header__top">
<div class="header__top-nav">
<div><a href="{{route('home')}}">Moje Konto</a></div>
</div>
<div class="header__top-nav">
<div><span>PLN</span></div>
</div>
</div>
<div class="header__mid">
<div class="header__logo">
<a href="{{route('index')}}"><img src="{{env('APP_URL')}}/public/images/icons/logo.svg" alt=""></a>
</div>
<div class="header__burger-button">
<img src="{{env('APP_URL')}}/public/images/icons/burger.svg" alt="">
</div>
<div class="header__user burger__item">
<div class="header__account">
<a href="{{ route('login') }}"><img src="{{env('APP_URL')}}/public/images/icons/account.svg" alt=""><img class = "white_icon" src="{{env('APP_URL')}}/public/images/icons/account_white.svg" alt=""></a>
<a href="{{ route('login') }}">Konto</a>
</div>
<div class="header__cart">
<a href="{{route("cart")}}"> <div class="cart"><img src="{{env('APP_URL')}}/public/images/icons/cart.svg" alt=""><img class = "white_icon" src="{{env('APP_URL')}}/public/images/icons/cart_white.svg" alt=""><div class="cart__items">{{\Cart::session($_COOKIE['cart_id'])->getTotalQuantity()}}</div></div></a>
<div class="header__cart-info">
<span>Razem</span><span class = "cart__price">{{$price}} PLN</span>
</div>
</div>
</div>
</div>
<div class="header__bottom">
@if(Request::url() === route('index'))
<div class="header__categories">
<div class="categories__button">
<img src="{{env('APP_URL')}}/public/images/icons/burger.svg" alt="">
<span>Categories</span>
<img src="{{env('APP_URL')}}/public/images/icons/arrow_down.svg" alt="">
</div>
<div class="categories__items border dropdown-menu">
<ul>
@yield("categories")
</ul>
</div>
</div>
@endif
<nav class="header__nav burger__item">
<ul>
<li><a href="{{route('contact')}}">Contact</a></li>
<li><a href="{{route('index')}}">Home</a></li>
<li><a href="{{route('products')}}">Wszystkie produkty</a></li>
</ul>
</nav>
</div>
</div>
</header>
@yield("content")
<footer class="footer">
<div class="container">
<div class="footer-top">
<div class="footer-top__socials">
<img src="{{env('APP_URL')}}/public/images/icons/instagram.svg" alt="" class="footer-top__socials-img">
<img src="{{env('APP_URL')}}/public/images/icons/tiktok.svg" alt="" class="footer-top__socials-img">
<img src="{{env('APP_URL')}}/public/images/icons/facebook.svg" alt="" class="footer-top__socials-img">
</div>
</div>
<div class="footer-mid">
<div class="footer-mid__info">
<form action="" class="footer-mid__info-form">
<input type="text" placeholder = "E-mail adress*">
<button type="submit">Sign-Up</button>
</form>
<div class="footer-mid__info-contact">
<h1>Contact US</h1>
<a href = "mailto:royalebrick@gmail.com">Royalebrick@gmail.com</a>
<a href = "tel:786038339">+48786038339</a>
</div>
</div>
</div>
<div class="footer-bottom">
<div class="footer-bottom__copyrights">
<p>© Copyright 2022</p>
<p>All rights reserved. Powered by Alexandru Muhai</p>
</div>
</div>
</div>
</footer>
<script src = "{{env('APP_URL')}}/public/js/main.min.js"></script>
<script>
$(document).ready(function(){
$(".product__button").click(function(event){
let id = parseInt($(this).children().text())
event.preventDefault()
addToCart(id)
})
})
function addToCart(id)
{
let qty = parseInt($('.cart__items').text())
let price = parseInt($(".cart__price").text())
qty ++;
$.ajax({
url:"{{route('addToCart')}}",
method:"POST",
data: {
id:id,
},
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
},
success:(data) => {
let totalPrice = price + data[id]['price']
$(".cart__price").text(totalPrice + " PLN")
$(".cart__items").text(qty)
}
})
}
</script>
@yield("script")
</body>
</html>