Search code examples
phplaravelcookiessession-cookiessetcookie

How to use cookies for getting multiple value on laravel 5.6?


Actually, I am new on cookies (haven't work on it) what I am trying to do is Whenever clients visit my site and read an article get the id of that read article (without login). whenever clients came back on the same site form the same device which he/she has visited Then hide article which has been already read show only not read articles.

I have to try to create cookies like this

Cookie::queue(cookie('key', 'value', $minute = 10));
request()->cookie('key');

but whenever I update value it only get only the latest value. so Is it possible to do it by using cookies? or is there any alternative that which I can use for making this possible?


Solution

  • Yes, it is possible.Instead of using the simple value you have to use an array of ids.

    Example:-

    Cookie::queue(Cookie::make('seen_posts', json_encode([1,2]), $minutes));
    

    Updating cookies

    $seen_posts = json_decode($request->cookie('seen_posts'),true);
    
    $seen_posts[] = 3;
    
    Cookie::queue(Cookie::make('seen_posts', json_encode($seen_posts), $minutes));