Published on

Laravel 8 to Laravel 10 Version Upgrade Experiment

When I first focused on learning laravel, the version of laravel that I used to learn was laravel version 8. After learning, I tried to implement it into several projects that I handled. And of course when the project is finished, the latest version of Laravel has been released. Because the latest version is usually updates and improvements from various sides, including security and bug fixes, of course it is better to use the latest version. Therefore, I tried to upgrade the laravel version in the project that I handled. Before upgrading directly, I experimented on a simple project and this will be the topic of discussion in this article.

Overview

In this experiment, we will try to update the laravel framework version from version 8 to version 10 on the sample project that has been prepared. During the update process, we will do two processes, namely

  1. The upgrade process from Laravel 8 to Laravel 9.
  2. The upgrade process from Laravel 9 to Laravel 10.

The final goal in this experiment is of course that the Laravel version is successfully upgraded and the simple project can be used.

Preparation

Before trying the process of updating the Laravel 8 version to the Laravel 10 version, there are several things that must be considered and of course must be prepared in advance. The following are the tools that I use.

  1. There are two versions of PHP that I use when trying this update, namely version 8.0 and version 8.1. For the php version switch process I have written in the previous post
  2. Composer version Composer version 2.3.5
  3. Project that will be used as a trial update process with Laravel version 8.
  4. Using phpunit for testing at the beginning and at the end.

Now we try to check the php version first. Open the terminal, then run the command below.

php -v

The output when the command is run.

PHP 8.0.30 (cli) (built: Aug 14 2023 06:42:40) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.0.30, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.30, Copyright (c), by Zend Technologies

The above output is the PHP version used at the beginning of the experiment.

Now we try to clone the project from the sample repository:

https://github.com/qadrLabs/belajar-laravel-8-testing-crud-feature

Open a terminal and clone this repository using git clone

git clone https://github.com/qadrLabs/belajar-laravel-8-testing-crud-feature.git

Next go to the project directory

cd learn-laravel-8-testing-crud-feature

Then we copy .env.example to .env

cp .env.example .env

Next we adjust the database credentials in this file.

Next we install the dependencies with the run command.

composer install

Then we generate the key using the command

php artisan key:generate

Next we run the command

php artisan migrate

Now we try to run tests first to make sure there are no errors.

vendor/bin/phpunit

The output when we run the above command.

$ vendor/bin/phpunit
PHPUnit 9.5.9 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 00:00.062, Memory: 22.00 MB

OK (2 tests, 4 assertions)

Okay, now we start our upgrade experiment from version 8 to version 9.

Step 1 - Upgrade Laravel version 8 to Laravel version 9

Now we open the composer.json file first. Then we adjust the php version according to the php requirements for Laravel version 9. Find the following line of code in the composer.json file.

"require": {
        "php": "^7.3|^8.0",

		// ... another line of code
    },

Then we change the php version to ^8.0.2.

    "require": {
        "php": "^8.0.2",

		// ... another line of code
    },

Next we change the version of laravel/framework and the library nunomaduro/collision.

    "require": {

        "laravel/framework": "^9.0",
        
        // ... another line of code
    },
    "require-dev": {
    
        "nunomaduro/collision": "^6.1",
        
	    // ... another line of code
    },

Then finally change the facade/ignition library with "spatie/laravel-ignition": "^1.0"

    "require-dev": {
        "spatie/laravel-ignition": "^1.0",
        
		// .. another line of code
    },

Save the composer.json file again.

Next we update using the command

composer update

Wait until the framework and library update process is complete.

When it's finished, we try to run the test again to make sure everything goes well.

vendor/bin/phpunit

Output:

vendor/bin/phpunit
PHPUnit 9.6.11 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 00:00.071, Memory: 24.00 MB

OK (2 tests, 4 assertions)

Okay there is no error or any sign that the upgrade process from laravel version 8 to laravel 9 is successful.

As an alternative, you can also open the project directly in the browser. Run command.

php artisan serve

Then open http://127.0.0.1:8000 in the browser. If there is no error, the upgrade process is successful.

Step 2 - Upgrade Laravel version 9 to Laravel version 10

Before continuing the process of upgrading the Laravel version, we must first change the PHP version used to meet the requirements for using Laravel version 10, namely PHP version 8.1 and above.

After the PHP version is changed, we check the php version used again to make sure.

php -v

Output when the command is run.

$ php -v
PHP 8.1.22 (cli) (built: Aug 14 2023 05:32:33) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.22, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.22, Copyright (c), by Zend Technologies

Okay the first requirement has been fulfilled.

The next requirement is that the composer used is composer 2.2.0 or greater.

Now we check the composer version.

composer --version

Output when the command is run.

$ composer --version
Composer version 2.3.5 2022-04-13 16:43:00

The above output is the composer that I used.

Okay the requirement for composer has also been fulfilled.

Now we continue the upgrade process from laravel version 9 to laravel version 10.

The first step is to adjust the PHP version according to the laravel 10 requirements. Open the composer.json file again,

    "require": {
        "php": "^8.0.2",

        // ... another line of code
    },

then we adjust the php version.

    "require": {
        "php": "^8.1",
        
        // ... another line of code
    },

The second step is to update the dependencies used. Find laravel and the libraries below.

    "require": {
        // ... another line of code
        "laravel/framework": "^9.0",
        "laravel/sanctum": "^2.11",
        
        // ... another line of code
    },
    "require-dev": {
        "spatie/laravel-ignition": "^1.0",

        // ... another line of code
    },

Then we customise the version and add the doctrine/dbal library.

"require": {

        "laravel/framework": "^10.0",
        "laravel/sanctum": "^3.2",
        "doctrine/dbal": "^3.0"

        // ... another line of code
    },
    "require-dev": {
        "spatie/laravel-ignition": "^2.0",

        // ... another line of code
    },

Since we are using phpunit for testing we need to customise the library too.

    "require-dev": {
        // ... another line of code
        
        "nunomaduro/collision": "^6.1",
        "phpunit/phpunit": "^9.3.3"
    },

We match the phpunit version and the nunomaduro/collision library.

    "require-dev": {
        // ... another line of code
        
        "nunomaduro/collision": "^7.0",
        "phpunit/phpunit": "^10.0"
    },

The next step is to modify the minimum stability version to stable.

"minimum-stability": "stable",

Since Laravel 10 core already provides CORS middleware, we remove the fruitcake/laravel-cors dependency.

    "require": {
        "php": "^8.1",
        "fruitcake/laravel-cors": "^2.0", // remove this
        "guzzlehttp/guzzle": "^7.0.1",
        "laravel/framework": "^10.0",
        "laravel/sanctum": "^3.2",
        "laravel/tinker": "^2.5",
        "doctrine/dbal": "^3.0"
    },

After the library is removed, here are the dependencies that we have customised.

    "require": {
        "php": "^8.1",
        "guzzlehttp/guzzle": "^7.0.1",
        "laravel/framework": "^10.0",
        "laravel/sanctum": "^3.2",
        "laravel/tinker": "^2.5",
        "doctrine/dbal": "^3.0"
    },
    "require-dev": {
        "spatie/laravel-ignition": "^2.0",
        "fakerphp/faker": "^1.9.1",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.2",
        "nunomaduro/collision": "^7.0",
        "phpunit/phpunit": "^10.0"
    },
    "minimum-stability": "stable",

Since there is code that uses the fruitcake/laravel-cors library, we need to remove its usage and adapt to laravel's code.

Open the app/Http/Kernel.php file, then find the following line of code.

    protected $middleware = [

        // ... another line of code      
        \Fruitcake\Cors\HandleCors::class, // find this class
        // ... another line of code    
    ];
        

Then we adjust it to the following line of code.

    protected $middleware = [        
        // ... another line of code
    
        \Illuminate\Http\Middleware\HandleCors::class,
       
       // ... another line of code
    ];

Save the app/Http/Kernel.php file again.The next step is to also update the third party libraries used. This is an optional step, because in the sample project there are additional libraries for testing so we try to update them too.Open the composer.json file again, then find the following line of code.

    "require-dev": {
    
        "laravel/browser-kit-testing": "^6.4",
        // ... another line of code
    },

We adjust to the version of the library that supports laravel 10.

    "require-dev": {        
        "laravel/browser-kit-testing": "^7.0",        
        // ... another line of code    
    },

Now we start the process of updating the framework and dependencies that we have customised with the run command

composer update

Wait until the update process is complete.

After completion, we try to run testing using the command.

vendor/bin/phpunit

The output when run turns out that there needs to be an adjustment to the phpunit configuration for the latest version.

vendor/bin/phpunit
PHPUnit 10.3.2 by Sebastian Bergmann and contributors.
Runtime:       PHP 8.1.22
Configuration: /home/fourze/app/testing-lab/laravel-10/learn-laravel-8-testing-crud-feature/phpunit.xml

..                                                                  2 / 2 (100%)

Time: 00:00.078, Memory: 26.00 MB

There was 1 PHPUnit test runner deprecation:

1) Your XML configuration validates against a deprecated schema.Migrate your XML configuration using "--migrate-configuration"!OK, but there were issues!Tests: 2, Assertions: 4, Deprecations: 1.

Now we try to customise the phpunit configuration with the run command.

vendor/bin/phpunit --migrate-configuration

The output in the terminal displays the process.

$ vendor/bin/phpunit --migrate-configuration
PHPUnit 10.3.2 by Sebastian Bergmann and contributors.Created backup:         /home/fourze/app/testing-lab/laravel-10/learn-laravel-8-testing-crud-feature/phpunit.xml.bak
Migrated configuration: /home/fourze/app/testing-lab/laravel-10/belajar-laravel-8-testing-crud-feature/phpunit.xml

Now we try to run phpunit again.

vendor/bin/phpunit 

Output:

vendor/bin/phpunit 
PHPUnit 10.3.2 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.22
Configuration: /home/fourze/app/testing-lab/laravel-10/learn-laravel-8-testing-crud-feature/phpunit.xml

..                                                                  2 / 2 (100%)Time: 00:00.076, Memory: 26.00 MB

OK (2 tests, 4 assertions)

Yes, there is no error when we run the test.This is a sign that the upgrade process from laravel version 9 to laravel version 10 was successful.

Of course, to make sure you can run the project in the browser directly.Run command.

php artisan serve

Then open http://127.0.0.1:8000/post in the browser. If there are no errors, this is a sign that the upgrade process is successful.

Now we check the laravel version using the command below.

php artisan --version

Output:

$ php artisan --versionLaravel Framework 10.19.0

Conclusion

this post we have tried to upgrade the laravel framework version. Starting from upgrading laravel version 8 to version 9, then continued with upgrading laravel version 9 to version 10. In accordance with the goals we set at the beginning of the experiment and the upgrade process was successful, testing using phpunit was successful.