1

I have updated my laravel version from 5.5 to 5.6 and followed the update guide but there is one error that I cannot figure out.

Error

Argument 2 passed to Symfony\Component\HttpFoundation\Cookie::__construct() must be of the type string or null, array given

My code generating this error:

public function show($id, DealService $dealService, CookieJar $cookieJar)
    {
        if(null != $coupon = $dealService->getActiveDealById($id))
        {
            if(!Cookie::has('recent'))
            {
                $ids = [];
                array_unshift($ids, $id);
                $cookieJar->queue('recent', $ids);
            }
            else
            {
                $ids = Cookie::get('recent');
                if(!in_array($id, $ids))
                {
                    array_unshift($ids, $id);
                    $ids[] = $id;
                    $cookieJar->queue('recent', $ids);
                }
            }
            if(!empty($ids))
            {
                $recent_deals = $dealService->getDealsByIds($ids);
            }
            $related_deals = $dealService->getRelatedActiveDeals($id);

            return view('couponia.show', ['coupon' => $coupon, 'recent_deals' => $recent_deals, 'related_deals' => $related_deals]);
        }
        else
        {
            return view('errors.404');
        }
    }

CookieJar queue method throws the exception as it is trying to create a Cookie instance, but this code used to work perfectly in 5.5 and the CookieJar documentation also suggests that it accepts array as argument.

  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – miken32 Oct 23 '18 at 22:40
  • I don't see where in this code you are instantiating a `Cookie` object. The PHP error will give you a line number, and Laravel will give you a backtrace. The error is pretty self-explanatory, you're passing an array somewhere that it isn't expected. – miken32 Oct 23 '18 at 22:42
  • what does your route look like? – wheelmaker Oct 23 '18 at 22:57
  • 1
    good read: https://github.com/symfony/symfony/issues/23017 – Wreigh Oct 24 '18 at 00:23
  • The cookie jar object tries to create a Cookie object which's value should be string not array, but this used to work... I understand that if I change it to be a string it will work but the documentation suggests that it should work ... @miken32 – Kristian Martirosyan Oct 24 '18 at 06:50
  • here is the reference to the class... https://laravel.com/api/5.6/Illuminate/Cookie/CookieJar.html – Kristian Martirosyan Oct 24 '18 at 07:03

1 Answers1

0

The Problem was that after the update the Symphony Library was also updated to version 3.3 which does not support array cookies anymore. At this point a solution could be to serialize the array and then unserialize.