How to upgrade to Laravel 8?

02 Jan 2022

Laravel 8 is now out! Check this upgrade guide to Laravel 8 from Laravel 7.

Prerequisites

PHP 7.3

In the previous version of Laravel, minimum PHP version was 7.2.5. However with the release of Laravel 8, at least PHP 7.3.0 is required.

In order to check your php version, open the terminal and type:

php -v

If the PHP version you see in the terminal is minimum 7.3.0 you are good to go for the next step, otherwise first update PHP to continue.

Laravel 7

To be able to upgrade to Laravel 8, make sure your current Laravel version is 7.x. It is not possible to upgrade from 6.x or previous versions. In order to check your project Laravel version, open the terminal in your project root and type:

php artisan --version

If your version is 7.x, let’s start to upgrade!

Update composer.json

Open your composer.json file and update the following dependencies as below:

guzzlehttp/guzzle to ^7.0.1
facade/ignition to ^2.3.6
laravel/framework to ^8.0
laravel/ui to ^3.0
nunomaduro/collision to ^5.0
phpunit/phpunit to ^9.0

Then open the terminal in your project root and perform:

composer update

By complating this step, now you have upgraded your core Laravel application to Laravel 8. In the following steps we will take a look what changes to make in your application specific files.

Seeders

There are two things needs to be done in this step.

First, rename the database/seeds directory to database/seeders.

Then open your each seeder class and add the below namespace to top of the file:

<?php

namespace Database\Seeders;

Factories

Factories were written from scratch in Laravel 8 to support classes and unfortunately, factories written in Laravel 7 are no longer compatible.

If you want to continue with your current factories you will need to install additional composer package to maintain backwards compatibility:

composer require laravel/legacy-factories

As more recommened but also more time consuming option (depends how many factories you have in your project), check Laravel 8’s core UserFactory.php , and rewrite your existing factories in class-based way. Also do not forget to add namespaces to them since factories are now namespaced like seeders.

<?php

namespace Database\Factories;

Autoload Seeders & Factories

If you performed adding namespaces to your Seeder and Factory classes, open your composer.json file again, delete classmap block from the autoload, then add the new namespace mappings:

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    }
},

If you stayed with Laravel 7’s approach in Factories, make the changes mentioned in this section accordingly.

Models

The models are housed in the app/Models path now instead of directly in app/ directory. If you want, you can leave it as it is, and Laravel 8 automatically detect you don’t have the new Models folder and will work without any problem.

However if you want to be more future versions compatible, create the Models directory, move your models in it, and make the changes on the files that you are calling your models.

# Instead of: use App\User;
use App\Models\User;

Pagination

The paginator in Laravel 8 uses Tailwind CSS styles by default. If you were using Bootstrap in your Laravel 7 application, you need to let Laravel know that you won’t use the default setting. So open your AppServiceProvider.php and add following lines:

use Illuminate\Pagination\Paginator;

Paginator::useBootstrap(); #Make sure, you are calling this method, from boot() method.

Routing

Update your RouteServiceProvider.php based on Laravel 8’s out of pack RouteServiceProvider.php When done, you will also need to make changes on routes/web.php

You can either choose to use PHP callable syntax or string syntax.

// Using PHP callable syntax...
use App\Http\Controllers\UserController;

// Using PHP callable syntax...
Route::get('/users', [UserController::class, 'index']);

// Using string syntax...
Route::get('/users', 'App\Http\Controllers\UserController@index');

If you have urls generated in your templates using action helper, you will also need to update your templates.

Depending on your project size this might give you a lot of hassle, and you might want to deal with the routes in Laravel 7 way. In order to do so just uncomment the namespace in your route service provider, and that’s all.

At first glance, new approach seems like more to write, and this is the only reason you want to keep things in Laravel 7 way, notice with the change, now you will be able to click through to the controller in IDEs.

More Info

In this article I have tried to outline how to upgrade Laravel. But your needs may differ depending on the scope of your project. Therefore, I strongly recommend that you also check Laravel’s official upgrade guide .

Comments (2)

  • Xoxo 02 Jan 2022 20:08:38

    Its very nice

  • draganbt 16 Jan 2023 10:59:31

    Great explanation. Thanks

Comment

Please sign in or sign up with one of below services to share your thoughts.