Categories
Fixing Stuff Laravel Samuel

Laravel Comapact PHP 7.3 Temporary Fix

With PHP 7.3 being automatically pushed out to a lot of servers recently we noticed a bug that came up on severeal older Laravel sites. The bug is:

compact(): Undefined variable: operator 

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;
    }

A Fix for this bug can be found on this stack overflow article: https://stackoverflow.com/questions/56726263/compact-undefined-variable-operator

The specific fix is:

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

you just remove the 'operator' parameter

Please note this is just a temporary fix to remove the error page. The long term fix for this is that the version of Laravel will need to be updated, and then composer update needs to be run to pull in the latest packages that will work with the new version of Laravel, and they will handle the new behavior of the compact method. The old version allowed for empty variables to be passed to the compact method, but in PHP 7.3 if an empty variable is passed to the compact method it presents you with that nice error message 🙂