Upgrade from Laravel 7.x to 8.0

Step 1:

Update the following dependencies in your composer.json file:

  • 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

old composer.json file

“require”: {
“php”: “^7.2.5”,
“fideloper/proxy”: “^4.4”,
“fruitcake/laravel-cors”: “^2.0”,
“guzzlehttp/guzzle”: “^4.3.1|^7.0.1”,
“laravel/framework”: “^7.29”,
“laravel/tinker”: “^2.5”
},
“require-dev”: {
“facade/ignition”: “^2.0”,
“fakerphp/faker”: “^1.9.1”,
“mockery/mockery”: “^1.3.1”,
“nunomaduro/collision”: “^4.3”,
“phpunit/phpunit”: “^8.5.8|^9.3.3”
},

Changed composer.json file

“require”: {
“php”: “^7.3”,
“fideloper/proxy”: “^4.4”,
“fruitcake/laravel-cors”: “^2.0”,
“guzzlehttp/guzzle”: “^7.0.1”,
“laravel/framework”: “^8.0”,
“laravel/tinker”: “^2.5”
},
“require-dev”: {
“facade/ignition”: “^2.3.6”,
“fakerphp/faker”: “^1.9.1”,
“mockery/mockery”: “^1.3.1”,
“nunomaduro/collision”: “^5.0”,
“phpunit/phpunit”: “^9.0”
},

Note : The following first-party packages have new major releases to support Laravel 8. If applicable, you should read their individual upgrade guides before upgrading:

In addition, the Laravel installer has been updated to support composer create-project and Laravel Jetstream. Any installer older than 4.0 will cease to work after October 2020. You should upgrade your global installer to ^4.0 as soon as possible.

Then open the terminal in your project root and perform:

composer update

Step 2:

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:

<?phpnamespace 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.

<?phpnamespace 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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store