Laravel 8 Project is not opening in the server..getting error in TestDatabases.php

Viewed 13444

I am Getting error when I open the laravel 8 project

 protected function switchToDatabase($database)
    {
        DB::purge();
 
        $default = config('database.default');
 
        config()->set(
            "database.connections.{$default}.database",
            $database,
        );
    }

"syntax error, unexpected ')'" in vendor/laravel/framework/src/Illuminate/Testing/Concerns/TestDatabases.php

7 Answers

You're using a PHP version lower than 7.3

in: vendor/laravel/framework/src/Illuminate/Testing/Concerns/TestDatabases.php

Change:

    if ($url) {
        config()->set(
            "database.connections.{$default}.url",
            preg_replace('/^(.*)(\/[\w-]*)(\??.*)$/', "$1/{$database}$3", $url),
        );
    } else {
        config()->set(
            "database.connections.{$default}.database",
            $database,
        );
    }

to:

    if ($url) {
        config()->set(
            "database.connections.{$default}.url",
            preg_replace('/^(.*)(\/[\w-]*)(\??.*)$/', "$1/{$database}$3", $url)
        );
    } else {
        config()->set(
            "database.connections.{$default}.database",
            $database
        );
    }

Removing the comma at the end of line fixes the issue.

Gert B. answer does solve the situation, but you should instead update your PHP to version 7.3 or higher, as Mohammad mentioned, to solve this. Changing platform/vendor code is not the best option.

remove the trailing "," in your set function call:

config()->set(
        "database.connections.{$default}.database",
        $database
    );

Remove the "," directly from file placed in vendors folder isn't a good idea.

To resolve the issue update your php version (7.3 or higher).

If you are using valet:

  • execute valet use php@7.3 or higher

If you are using something like vagrant (and apache):

  • connet to server via "vagrant ssh"
  • install php7.3 or higher (sudo apt install php7.3) and then the related packages that you want
  • disable your current php version via "sudo a2dismod php7.2" (if your current version is 7.2)
  • enable php7.3 via "sudo a2enmod php7.3"
  • restart apache "sudo service apache2 restart"

phpinfo() gives you the version of apache which is the actual version the project runs on and in case you want to change it simply follow these steps:

install php version that you wish to install:

sudo add-apt-repository ppa:ondrej/php -y
sudo apt-get update

//replace X with the version you want
sudo apt-get install php7.X-fpm php7.X-curl php7.X-mbstring php7.X-mysql -y

Now restart your apache:

sudo service apache2 restart

disable current php version(the one that phpinfo() gives you):

sudo a2dismod php7.2

And now enable php version that you just installed:

sudo a2enmod php7.X

In case anyone is as stuck on this as I was, here were the steps I followed. If you have the same situation as me, your php versions might be different. I was going from 7.2 to 7.4. I had to:

  • Uninstall my old version of php via homebrew

brew uninstall php@7.2

  • Completely delete the reference I had to that old version of php even though I had uninstalled it via homebrew.

rm -rf /usr/local/Cellar/php@7.2

  • Unlink all versions except php@7.4. For me that was php@7.2 and php with no version specification.

brew unlink php

brew unlink php@7.2

For my experience in Ubuntu Linux, check your php version

php -v

You may switch to require version, install if you don't have php@7.4

sudo update-alternatives --set php /usr/bin/php7.4

Do your requirements. I have migration issue. So

php artisan migrate

switch back to default version

sudo update-alternatives --set php /usr/bin/php7.2
Related