Upgrade laravel to 5.7 [Step by Step Guide]

2237
Upgrading laravel application from 5.6 to 5.7

In this article, we will discuss how to upgrade laravel 5.6 application to laravel 5.7. Laravel is one of the best PHP framework. Currently, laravel has a very large active communiny. Laravel has been continuously improving and adding new features since its first release. The latest version of laravel is 5.7. All of the new features explained in details can be found in this article. If you want to dig dig in more detail about creating application with laravel 5.7, you can read this article.

Upgrading to laravel 5.7 need some pre-requisities like PHP >= 7.1.3, OpenSSL PHP Extension, PDO PHP Extension, Mbstring PHP Extension, Tokenizer PHP Extension, XML PHP Extension, Ctype PHP Extension and JSON PHP Extension.

Below are the steps you need to follow to upgrade to laravel 5.7 from 5.6.

Update Dependencies

The first step to upgrade laravel/framework dependency in the composer.json file. In this composer.json file, upgrade laravel 5.6 to laravel 5.7. If you are using Laravel Passport, you should update your laravel/passport dependency to ^7.0 in your composer.json file.

"require": {
    .....
    "laravel/framework": "5.7.*",
    .....
},

Similarly, we need to replace the version of third party dependencies to * so that composer will install the compatible version of the packages used automatically.

"require": {
    .....
    "cartalyst/sentinel": "*",
    "intervention/image": "*",
    "laravel/socialite": "*",
    "thujohn/twitter": "*"
    .....
},

After changing these dependencies, we need to run composer update command in the terminal. The composer will now update your our laravel application to the specified version along with all the packages.

composer update

Update Application

There are various application level update in laravel 5.7. After updating dependencies, we need to check the below traits and methods.

Blade or operator

We used to write or in our blade file to define default value in laravel 5.6. But in laravel 5.7, it has been removed. Rather, PHP’s built-in ?? “null coalesce” operator has been introduced with same functionality. You can read more information about this PHP features in this article.

// Laravel 5.6...
{{ $name or 'Kodementor' }}

// Laravel 5.7...
{{ $name ?? 'Kodementor' }}

Routing

The Route::redirect method now returns a 302 HTTP status code redirect. The 302 HTTP status code refers to moved temporarily. It tells the client to look at (browse to) another url. The permanentRedirect method has been added to allow 301 redirects.

// Return a 302 redirect...
Route::redirect('/foo', '/bar');

// Return a 301 redirect...
Route::redirect('/foo', '/bar', 301);

// Return a 301 redirect...
Route::permanentRedirect('/foo', '/bar');

Cache

A new directory for cache has been added in storage/framework/cache directory in laravel 5.7. We need to create this directory manually. After creating the directory, a need file .gitignore should be added.

The register Method

The options argument in register method of Illuminate\Foundation\Application class has been removed. If you are overriding this method, you should update your method’s signature:

// laravel 5.6
public function register($provider, $options = [], $force = false)

// laravel 5.7
public function register($provider, $force = false);

The Authenticate Middleware

The authenticate method in laravel 5.6 used to only accept array $guards argument. But in laravel 5.7, it accepts $request as first argument. If there has been some change in this middleware, we need to update the signature.

// Illuminate\Auth\Middleware\Authenticate

// laravel 5.6
protected function authenticate(array $guards)

// laravel 5.7
protected function authenticate($request, array $guards)

The ResetsPasswords Trait

The sendResetResponse method in ResetsPasswords trait used to accept only $response as a parameter. But in laravel 5.7, it now accepts the incoming Illuminate\Http\Request as its first argument. If you are overriding this method, you should update your method’s signature:

// Illuminate\Foundation\Auth\ResetsPasswords

//laravel 5.6
protected function sendResetResponse($response)

//laravel 5.7
protected function sendResetResponse(Request $request, $response)

The SendsPasswordResetEmails Trait

The sendResetLinkResponse method in SendsPasswordResetEmails trait used to accept only $response as a parameter. But in laravel 5.7, it also now accepts the incoming Illuminate\Http\Request as its first argument. If you are overriding this method, you should update your method’s signature:

// Illuminate/Foundation/Auth/SendsPasswordResetEmails

//laravel 5.6
protected function sendResetLinkResponse($response)

//laravel 5.7
protected function sendResetLinkResponse(Request $request, $response)

The Gate Contract

The raw method was changed from protected to public visibility. Any class implementing this interface should add this method to your implementation.

//Illuminate/Auth/Access/Gate

//laravel 5.6
protected function raw($ability, $arguments = []);

//laravel 5.7
public function raw($ability, $arguments = []);

The softDeletesTz Migration Method

The softDeletesTz method in Illuminate\Database\Schema\BluePrint class used to accept only $precision as a parameter. But in laravel 5.7, it also now accepts the column name as its first argument.

//Illuminate\Database\Schema\BluePrint

//laravel 5.6
public function softDeletesTz($precision = 0)

//larave. 5.7
public function softDeletesTz($column = 'deleted_at', $precision = 0)

The ConnectionInterface Contract

The Illuminate\Contracts\Database\ConnectionInterface contract’s select and selectOne method signatures have been updated to accommodate the new $useReadPdo argument:

public function selectOne($query, $bindings = [], $useReadPdo = true);

public function select($query, $bindings = [], $useReadPdo = true);

Email Verification

When using Laravel’s new email verification feature, you will need to add additional scaffolding to your application. First, add the VerificationController to your application: App\Http\Controllers\Auth\VerificationController.

You will also need to modify your App\User model to implement the MustVerifyEmail contract. More information can be found in about sending verification email after registration in laravel 5.7 in this article.

These are some of the key points to be noticed during upgrade from laravel 5.6 to laravel 5.7. You can find all the list in this documentation.

Read More Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.