Laravel 5.6: ErrorException: compact(): Undefined variable: operator

2k views Asked by At

I am upgrading my laravel from 5.5 to 5.6. and upon running tests, I am getting this error:

ErrorException: compact(): Undefined variable: operator

Now there is a solution present here Laravel - compact(): Undefined variable: operator in Builder.php but I am following that very solution and still facing that error. Earlier I was upgrading from 5.4 to 5.6 then upon seeing the solution mentioned in the above question, I upgraded from 5.4 to 5.5 and now trying to upgrade from 5.5 to 5.6.

Here is a little piece of code that is causing this error:

$query = TransportOrder::with('orderNumber');

$query->whereHas('orderNumber', function ($q) use ($orderNumber) {
      $q->where('order_number', '=', $orderNumber);
});

return $query->first();

That whereHas is causing this problem. So is there an alternate to whereHas and with or there is any other solution that I am missing?

My php version is this:

PHP 7.3.23-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Oct  6 2020 11:36:27) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.23, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.23-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
    with blackfire v1.24.2~linux-x64-non_zts73, https://blackfire.io, by Blackfire

EDIT:

compact() is used in vendor class Builder:

public function addWhereExistsQuery(self $query, $boolean = 'and', $not = false)
    {
        $type = $not ? 'NotExists' : 'Exists';

        $this->wheres[] = compact('type', 'operator', 'query', 'boolean');

        $this->addBinding($query->getBindings(), 'where');

        return $this;
    }
2

There are 2 answers

0
Saani On BEST ANSWER

As it turns out upgrading laravel to 6.5.40 did the trick, as the mentioned issue was not resolved in the earlier versions.

0
Eneki On

If:

  1. You can't upgrade Laravel for whatever business/team related reason and
  2. Are sure that the Homestead box is running the correct version of PHP (7.2 or lower based on the answer given here) and yet
  3. are still getting this error,

explicitly define the PHP version on the Homestead.yaml file. This should be done on the sites property as such (each is a new line, of course):

map: test.appp /home/vagrant/code/test/public php: "7.1"