Categories
Laravel Samuel

Upgrading Laravel From 5.6 -> 5.7 -> 5.8 -> 6.0

This post is going to go through upgrading a site currently running Laravel 5.6. We will be taking the site up from 5.6 to 5.7 with the goal of eventually going all the way to 6.0 (the latest LTS at the time of this writing).

Start by creating a new upgrade branch from the dev branch:

git checkout -b feature/upgrading-laravel dev

*please note we are pulling from the dev branch here because the dev branch is currently more up to date than the master branch. We are writing these Laravel upgrades during a single day upgrade process, taking a Laravel site from 5.5 all the way up to 6.0. So if you are following along this series updating your own site make sure to branch off of whatever your current branch is (which will most likely be master)

The next thing we are going to do is open up sourcetree and push our newly created feature branch up to the remote repo server. To do this we will hit the push button and then check the box for the new branch and hit push.

Now that we have our new feature branch to the remote repo, were going to login to Laravel shift and purchase the shift we need, in this case we will be purchasing a shift from 5.6 -> 5.7. When we purchase the shift we will input our new feature branch as the branch shift should use to update from. In the Shift Screen there will be 2 boxes and we will need to fill them in this:

username/repo-name branch-name

With our shift purchased and executed, its time to merge the new branch shift creates into our updating branch and check how the site is doing locally:

Git fetch origin
Git checkout shift-42238
Git checkout feature/upgrading-laravel
Git merge shift-42238

Before we view the site locally we will also want to run:

composer update
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan config:clear

Issues with cache:clear:
*If the artisan cache:clear line fails due to permissions issue, then do the following to fix that error:

Check to see if the data folder is present at: 
storage/framework/cache/data

Create the data directory manually if its not there: (storage/framework/cache/data)

run php artisan route:clear again and it should work :)

--update --
If creating the data folder still does not fix the issue, then try running:

php artisan config:cache 

If there are any bugs we will fix those, in this case we did not have any issues so we will go ahead and merge our branch into the dev branch and test it on the staging server.

git checkout dev
git merge --no-ff feature/upgrading-laravel -m "merge laravel 5.7 into dev"

*we can either then git push dev from the command line, or we can use sourcetree to push the dev branch up to the remote server, and deploy to the staging site from there.

Once logged into our staging server we will run our deployment script, and then navigate to the project root folder and run the following commands:

composer update
php artisan cache:clear
php artisan route:clear
php artisan view:clear

*one special note, if the server has any issue running composer update, we can always delete the vendor directory and then run composer install which will install a fresh version of all dependencies. The fastest way to delete the vendor folder is via command line with:

rm -rf vendor

Fixing Issues:

#1: A Note From Shift: Laravel 5.8 changed several of the core contracts with new implementations and methods. You should review the Upgrade Guide for more detail on these respective changes:

Shift found references to these contracts within:

  • app/Excel/Exports/InquiryExport.php

Error Message On Staging Server Command Line:

In CustomFormBuilderServiceProvider.php line 9:

Interface 'Illuminate\Contracts\Support\DeferrableProvider' not found

In CustomFormBuilderServiceProvider.php line 9:

Interface 'Illuminate\Contracts\Support\DeferrableProvider' not found

To fix this issue we just updated the packages in composer.json to work with the latest version of Laravel. This was tricky as composer was not able to resolve the packages into an installable set, so what we did is removed all of the packages and then added them back into the file in small chunks and ran composer each time, until we arrived at the full set of needed packages.

Error #2:

The file TrustProxies.php was causing an error message to display in the command line, to fix this we changed the code in this file to:

protected $headers = Request::HEADER_X_FORWARDED_ALL;

Error #3:

Now we were at the point where we could actually pull the website up, but we received the following error screen on any page of the site:

To Fix this issue we went to the file referenced in the error message (configure_vehicle.blade.php) and we updated the code to:

OLD: 
-    var base_url = "{{ route('configurator.configure_vehicle', ['manufacturer' => $manufacturer->slug, 'vehicle' => $vehicle->slug]) }}";

New:   var base_url = "{{ route('configurator.configure_vehicle', ['manufacturer_slug' => $manufacturer->slug, 'vehicle_slug' => $vehicle->slug]) }}";

The latest version of Laravel needs these values to be explicitly defined.

Error #4

Once we try to login to the site we are getting the error:

To fix this we will update our call in AuthAdminiter.php to be:

$this->authenticate($request, $guards);

Error #5

Once we got the login permission issue fixed, we were then presented with this error screen:

If we look at the error we can pull out a few clues:

Missing required parameters for [Route: packages.create_for_vehicle] 
[URI: manage/packages/create/vehicle={vehicle_id}]. 
(View:C:\wamp64\www\SFPS\resources\views\vehicles\partials\table.blade.php)

The route mentioned in the error wants you to pass vehicle_id, but the code as-is is only passing in id so Laravel can’t figure that out. To fix this we can either rename it, or remove the special key. ([$vehicle->id]  or [‘vehicle_id’=>$vehicle->id]). So in our case we made the following update to fix the bug:

Old: 
<td>{{ Helper::number(count($vehicle->packages)) }} {!! link_to_route('packages.create_for_vehicle', 'Add', ['id' => $vehicle->id], ['class' => 'btn btn-primary']) !!}</td>

New:         
<td>{{ Helper::number(count($vehicle->packages)) }} {!! link_to_route('packages.create_for_vehicle', 'Add', ['vehicle_id' => $vehicle->id], ['class' => 'btn btn-primary']) !!}</td>

Error #6

It looks like Laravel dropped support for SparkPost around 5.8 – so we are now getting these errors on all of our form submissions:

To fix this we need to install this package:
https://github.com/vemcogroup/laravel-sparkpost-driver

This package was giving us some issues when installing, in order to successfully install it we had to remove the 4.0 version that installed automatically with the package and instead install version 2.0 using the code below::

composer remove vemcogroup/laravel-sparkpost-driver


composer_memory_limit=-1 composer require vemcogroup/laravel-sparkpost-driver:^2.0

When we did this we ran into an issue that Guzzle 7.0 was not compatible with V2.0 of the SparkPost Package and could not install correctly as well, so to fix that we ran:

composer_memory_limit=-1 composer require guzzlehttp/guzzle:6.5.5

Once that package is installed its important to note that we also need to then run:

php artisan config:clear
php artisan cache:clear

The above gets the site working on our local machine, however when we deploy to the staging site we are still running into a different error screen which we will fix under Error #8 below, but first a general note.

General Error – Error #7

If you are getting the php memory limit error when running composer install or update, which looks like this:

To work around this issue run the code below, which tells PHP to keep trying instead of stopping after reaching the initial memory limit set on the server

composer_memory_limit=-1 composer install

Error #8:

On the staging server we are getting the error that allow_url_fopen must be enabled in php.ini. We login to cpanel and set this to enabled, but we still receive this message. So we will manually run the command below instead, which allows url_fopen for this 1 single command:

which composer

//for us this spits out /opt/cpanel/composer/bin/composer so we will then run

php -d allow_url_fopen=on /opt/cpanel/composer/bin/composer install

Error 9:

With the SparkPost API now working, we are getting the error below:

Client error: `POST https://api.sparkpost.com/api/v1/transmissions` resulted in a `400 Bad Request` response:
{\"errors\":[{\"message\":\"Unconfigured Sending Domain <example.com>

To fix this error we will open up config/mail.php and change the backup email address from hello@example.com to the actual address we want to use:

'address' => env('MAIL_FROM_ADDRESS', 'ActualEmailAddress'),