2181

How to upgrade website or custom web app from Laravel 9.x to Laravel 10.x?

By: Skynet Technologies USA LLC
8 mins
500
how to upgrade laravel 9 to 10

Laravel 10 was released on February 14, 2023, and every website and application that is running on Laravel 9 is expected to upgrade to the latest version. Laravel 9 will receive security updates till February 2024 and bug fixes till August 2023, thereby it is wise to upgrade your system with the new stable release as soon as possible to maintain the similar performance of the website or custom application.

Upgrading might not be difficult if your website/application is already on the last minor update of Laravel 9.x. And even if it takes some time, upgrading must be done in order to stay competitive. Read along to know what all upgrades your current system requires to use the new Laravel 10.x.

YOU MAY ALSO LIKE: Laravel 10 Release note and New Features

Facets that need upgrade from Laravel 9.x to Laravel 10.x

1. Dependencies update

A. PHP 8.1 + required

Laravel 10.x has a PHP minimum requirement of 8.1.0 or above. Thus, if you are using older versions of PHP, update it with the required version. Easy way to know the PHP version on which your system is running is by using below written command-

./vendor/bin/sail php -v
// or
./vendor/bin/sail php --version
//If you are not using sail
php -v

B. Composer 2.2.0 is required

To update Composer, you need to update some dependencies in your application’s composer.json file.

laravel/framework to ^10.0
laravel/sanctum to ^3.2
doctrine/dbal to ^3.0
spatie/laravel-ignition to ^2.0
laravel/passport to ^11.0 (Upgrade Guide)

There are upgrade guides available for Laravel packages. For instance, if you are upgrading from Sanctum 2.x to 3.x, you can refer to its upgrade guide. Likewise, for Laravel Sail and other packages, follow the documentation and upgrade accordingly.

Moreover, delete the processUncoveredFiles attribute from the section of application’s phpunit.xml configuration file if you want to use PHPUnit 10. After deleting it, update the below dependencies in the application’s composer.json file –

nunomaduro/collision to ^7.0
phpunit/phpunit to ^10.0

And then update other third-party packages if there are any consumed by application or website. They all must be on the precisely correct version to support Laravel 10.x.

2. Minimum Stability

The minimum stability setting must be ‘stable’ in app’s composer.json file and if it is by default ‘stable’, then it can be deleted the settings from your application’s composer.json file.

"minimum-stability": "stable",

3. Web application update

If it has a customized ‘public path’ for web application or website utilizing a binding path.public into the container, then it must update the code to invoke the usePublicPath method offered by the Illuminate\Foundation\Application object.

app()->usePublicPath(__DIR__.'/public');

4. Cache update

For improved performance and better storage efficiency, Redis cache tag support is required to be rewritten. Before Laravel 10.x, other releases used to accumulate stale cache tags in the cache while using Redis as the application’s cache driver.

To remove stale cache tags properly, Laravel’s new cache:prune-stale-tags Artisan command is expected to invoke in the application’s App\Console\Kernel class.

$schedule->command('cache:prune-stale-tags')->hourly();

YOU MAY ALSO LIKE: Laravel Packages

5. Database update & Database expression

For additional functionalities, Database expressions are required to be rewritten. Database expressions are generated via DB::raw. One thing to note is that expression’s getValue (Grammar $grammar) method can retrieve grammar’s raw string value. And in Laravel 10, casting an expression to a string using (string) is no longer supported.

If a website or web application is casting database expressions to string using (string) manually or invoking the _toString method on the expression directly, then it must update the code to invoke the getValue method.

use Illuminate\Support\Facades\DB;
$expression = DB::raw('select 1');
$string = $expression->getValue(DB::connection()->getQueryGrammar());

6. Query Exception Constructor

In Laravel 10.x, the Illuminate\Database\QueryException constructor accepts a string connection called as its first argument. So, if a custom web application or website is manually throwing this exception, adjust the code accordingly.

7. ULID Columns

While upgrading from Laravel 9.x to Laravel 10.x, if migration invokes the ULID method without any arguments then the column is called ULID. Previously it was named UUID.

$table->ulid();

If it requires to specify a column name explicitly while invoking the ULID method, pass the column name to the method.

$table->ulid('ulid');

8. Eloquent update

A. Model ‘dates’ property

Laravel 10.x removed the eloquent model’s deprecated $dates property and the custom application/website will use the $casts property.

protected $casts = [
  'deployed_at' => 'datetime',
];

B. Relation Method

toBase method has removed the getBaseQuery method on the Illuminate\Database\Eloquent\Relations\Relation class.

9. Logging update

A. Monolog 3

In the latest Laravel 10.x, its Monolog dependency is now updated to Monolog 3.x. Thus, it must upgrade Monolog if it is directly interacting with it in the application/website. (Refer to the upgrade guide)

Here you should know that if the website or web application is using any third-party logging services (Rollbar, BugSnag, or anything else), upgrade those services to the version that supports Monolog 3.x and Laravel 10.x.

10. Queues update

Two deprecated methods Bus::dispatchNow and dispatch_now have been removed in Laravel 10.x. So, now the custom application or website will use Bus::dispatchSync and dispatch_sync methods respectively at the place of previous methods.

YOU MAY ALSO LIKE: Laravel security best practices

11. Routing update

A. Middleware Aliases

Laravel 10.x has been renamed App\Http\Kernel class’ property $routeMiddleware to $middlewareAliases to define it precisely. Though it is not important to rename this property name.

B. Rate Limiter Return Values

When invoking the RateLimiter::attempt method, now returned value will be returned by the method on the provided closure. If the return value is null, then the method will return true:

$value = RateLimiter::attempt('key', 10, fn () => ['example'], 1);
$value; // ['example'] 

C. The Redirect::home Method

In Laravel 10.x, the deprecated method Redirect::home is eliminated and now applications/websites are expected to redirect to an explicit names route –

return Redirect::route('home');

12. Testing update

Laravel framework removed the MocksApplicationServices trait in the new version, which was deprecated in Laravel 9. This eliminated trait had testing methods expectsEvents, expectsJobs, and expectsNotifications. Since the trait is no longer in existence now; thus, it is suggested to change the above methods with Event::fake, Bus::fake, and Notification::fake respectively.

13. Validation update

If writing closure-based custom validation rules invokes $fail callback more than once, then it will append the messages to an array instead of overwriting the previous message. However, it will not impact the application or website.

Also, now $fail callback returns an object.

If in Laravel 9, it was type-hinting the return type of your validation closure, then it may require to update the type-hint

public function rules() {
  'name' => [
    function ($attribute, $value, $fail) {
      $fail('validation.translation.key')->translate();
    },
  ],
}

Additional updates

For upgrades, it should also check more changes and updates in the Laravel GitHub repository. However, some of the updates are not required currently, still, there is no harm in keeping these files in sync with website or custom application.

The GitHub comparison tool can help you view all the updates. Some changes are backward compatible and are optional for the upgrade from Laravel 9.x to Laravel 10.x.

Wrapping up

Read the Laravel 10.x documentation carefully and upgrade website or custom application accordingly. The sooner it will be upgraded, the lesser chance of a technical crisis will occur.

Skynet Technologies is a leading Laravel development company that specializes in providing comprehensive Laravel development services. Our expertise spans across various areas, including Laravel customization, Restful application development, migration, integration, extension development, Laravel ecommerce development, template design, as well as maintenance and support.

Whether you need a custom Laravel web app or a dedicated team of Laravel developers, we have you covered. Our skilled professionals can create high-end Laravel custom web applications tailored to meet your specific requirements. Fill out form below to request a free quote or send mail us at [email protected].