What’s New in Laravel 10: A Deep Dive Into the Latest Updates and Features

After over a decade since the introduction of the wildly popular Laravel framework, have you ever wondered, “What else can Laravel offer PHP developers?”

Given how much it has already simplified PHP development for both beginner and professional developers, some may even argue that it has spoiled developers into believing that PHP is the easiest programming language out there.

So, does Laravel still have more surprises in store for Laravel developers? Or has it exhausted all possible means to support PHP developers?

Well, at the very least, we know that Laravel 10 has a lot to offer. This is what we’ll uncover in this article as we take you on a journey through Laravel 10’s new features, fixes, and freshly deprecated methods and packages.

Laravel Release Schedule

The Laravel core team used to release two major versions yearly — one every six months.

However, the release cycle was changed when Taylor Otwell, the creator of Laravel, announced that a single major version would now be released each year. This enabled the core team and community to devote more time and effort to a specific version of the framework and introduce new powerful features without making any breaking changes.

With Laravel 9 released on February 8, 2022, the expected release schedule is as follows:

  • Laravel 10: February 7, 2023
  • Laravel 11: February 6, 2024

In addition, according to the support policy, bug fixes are offered for 18 months and security updates for two years for all Laravel versions.

The following are the expected bug fixes and security updates schedule:

  • Laravel 9 will continue to get bug fixes until August 8, 2023 and security fixes until February 6, 2024.
  • Laravel 10 will get bug fixes until August 6, 2024 and security fixes until February 4, 2025.
  • Laravel 11 is expected to get bug fixes until August 4, 2025 and security fixes until February 2, 2026.

Should You Upgrade to Laravel 10?

It’s important to remember that we don’t always need to upgrade our application’s Laravel version to the latest version as soon as a new version gets released.

Laravel is an open-source framework, which implies that every time we install a new Laravel instance on our machine, we own the framework’s codebase. This means that even if the framework version our app is using is no longer supported, the app will still work; we’ll just have to maintain it ourselves.

As a result, it is widely suggested that application stability be prioritized over framework upgrades.

In short, you should consider upgrading to Laravel 10 when:

  • The application is stable with its current version and functioning without problems.
  • The new version either adds a feature that your application requires or fixes an issue that your application is experiencing.
  • The application will be well tested before the upgrade changes are pushed into production.

Laravel 10 Hot Updates

Laravel 10 Logo
An image of the Laravel 10 Logo.

As you already know, Laravel 10 has not yet been released. However, we will keep this article updated with all fresh information about the anticipated release. Therefore, we recommend bookmarking this page and revisiting it from time to time.

New Features and Updates in Laravel 10

There’s no doubt that the most exciting part about any new release is the addition of new features. So without further ado, let’s start by having a look at the new features and updates in Laravel 10.

1. PHP 8.1: At the Heart of Laravel 10

PHP 8.1 is the minimum-required PHP version in Laravel 10. Based on the comparison between the Laravel 9 branch and the framework’s master branch on GitHub, some PHP 8.1 features, such as readonly properties and array_is_list, are expected to be introduced in Laravel 10.

2. Support for PHP 8.2

PHP 8.2 was released on December 8, 2022, only two months before Laravel 10 release date. Yet, this shouldn’t stop you from utilizing PHP 8.2 features, as without doing anything extra, Laravel 10 will be ready for PHP 8.2.

In fact, the entire Laravel ecosystem, including Forge, Vapor, and Envoyer, supports PHP 8.2, and you can even use PHP 8.2 with Laravel 9. How cool is that?!

3. Laravel Starter Kits Upgrade

Laravel Breeze and Jetstream are ready to use Laravel 10 once it gets released. In addition, they are already upgraded to Inertiajs 1 as well with a surprise for JetStream to have complete dark-mode support.

4. Predis Version Upgrade

Predis is a robust Redis client for PHP that may help you get the most out of caching to provide a fantastic user experience. Laravel formerly supported both versions 1 and 2, but as of Laravel 10, the framework no longer supports Predis 1.

Although Laravel documentation mentions Predis as the package for interacting with Redis, you may also use the official PHP extension. This extension provides an API for communicating with Redis servers.

5. Native Type Declarations

Laravel used to utilize DocBlocks in its skeleton code to clarify what a piece of code does and what sorts of parameters or responses to expect. However, thanks to native type declarations in Laravel 10, this will change.

The best way to explain this change is with a simple example. Instead of a function looking like this:

/**
* Determine whether the user can create models.
*
* @param  /{{ namespacedUserModel }}  $user
* @return /Illuminate/Auth/Access/Response|bool
*/
public function create({{ user }} $user)
{
    //
}

…it will look like this instead:

/**
* Determine whether the user can create models.
*/
public function create({{ user }} $user): bool
{
    //
}

This change is purely for the benefit of the developer experience, as IDEs will know the shape of the expected parameter and response. It will provide better type clarity when not possible through PHP native types. Hence, It will help the code editors to better perform with auto-complete features.

6. All Validation Rules Invokable by Default

If you were to make an invokable validation rule in Laravel 9, you would need to add --invokable flag after the artisan command. This is no longer necessary because all Laravel 10 rules are invokable by default. So, you may run the following command to create a new invokable rule in Laravel 10:

php artisan make:rule CustomRule

7. Native Column Modification Support

In an attempt to eliminate the need for doctrine/dbal package when using change() to modify columns, a new feature is coming to Laravel 10. This feature will allow developers to use change() method and modify columns on MySQL, PostgreSQL, and SQL Server without the need for extra packages. This is a significant and risky breaking change, but we believe it is worthwhile since it will take away the necessity for an additional package.

To have a better understanding of the new feature, see the example below:

$table->integer('user_balance')->unsigned()->default(0)->comment('balance'); // `user_balance` is an integer, unsigned, defaults to '0', and column comment is 'balance'

Now, we’re assuming that we have a column for user_balance and we want to change its type. Starting from Laravel 10 we can simply do this:

$table->bigInteger('user_balance')->change(); // This will change `user_balance` to bigInteger instead of just integer

The above code will successfully change the type of the column, but will also drop UNSIGNED, DEFAULT and COMMENT attributes. Therefore, it’s important to remember adding all the attributes when you’re changing the type of a column:

$table->bigInteger('user_balance')->unsigned()->default(0)->comment('balance')->change();

In the case where you have multiple database connections and have already installed doctrine/dbal, it’s recommended to call the Schema::useNativeSchemaOperationsIfPossible() method within the boot method in App/Providers/AppServiceProvider to be able to use native schema operations and to use native operations before relying on the package (SQLite, for example, does not yet support this):

use IlluminateSupportFacadesSchema;
 
class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Schema::useNativeSchemaOperationsIfPossible();
    }
}

8. Column Type Native Retrieval

Another noteworthy feature of Laravel 10 is the ability to use the Schema::getColumnType method without having to rely on the doctrine/dbal package. We currently use Schema::getColumnType with doctrine/dbal to obtain the column type. doctrine/dbal maps every native column type to its doctrine/dbal type equivalent, and it does not support many of the column types used by Laravel across various databases.

In Laravel 10 on the other hand, the new Schema::getColumnType method will return the actual column type rather than its doctrine/dbal equivalent. It also enables you to write integration tests for the new native column modifying feature. You may use this feature to get either the data type name or the whole type definition of the specified column:

Schema::getColumnType('products', 'price'); // decimal

9. Faster Hashing Algorithm

xxHash is a Hash algorithm that is incredibly fast. It features great output randomness and dispersion, as well as uniqueness to reduce collisions. Since PHP 8.1 provides support for xxh128, and Laravel 10 runs on PHP 8.1, having such a reliable hash algorithm within Laravel 10 is ideal.

It’s worth mentioning that Taylor highlighted during his review of this change that some third-party packages may rely on the file names being in the exact format as the SHA-1 hash, which is the algorithm Laravel used to use for hashing. Therefore, If you’re planning an upgrade to Laravel 10, it would be wise to double-check this in any third-party packages you’re using in your app.

10. whereExists() Method Support for Eloquent Builder

Currently, using whereExists() requires configuring the nested query using a closure. Fortunately, with Laravel 10, it is now possible to include an Eloquent Builder as a nested query. It enables the usage of custom builder methods, model scopes, and so on.

For instance, we normally do this if we want to use whereExists():

Order::whereExists(function ($query) {
    $query->from('products')->whereColumn('products.order_id', 'orders.id');
});

With Laravel 10, we can do just this instead:

Order::whereExists(
    Product::whereColumn('products.order_id', 'orders.id')
);

11. Eager Loading Optimization

One of the interesting new features of Laravel 10 is eager loading optimization when there aren’t any keys to be loaded. This change is more of a fix than a feature since it tackles a current issue in which eager loading relations causes a large number of impossible queries to be executed.

Currently when eager loading relations that doesn’t have any keys to be loaded, Laravel will still execute a query similar to this one select /* from `table_name` where 0 = 1. However, the new Laravel 10 update checks to verify whether there are any keys available in the first place, and if not, provides an empty collection, eliminating the need for the unnecessary database queries.

Deprecated Methods and Packages in Laravel 10

Laravel 10 Says Goodbye to PHP 8.0

Laravel framework will drop support for PHP 8.0 in Laravel 10. Hence, If you’re planning to upgrade your app to Laravel 10, you must first update the PHP version to PHP 8.1 or PHP 8.2.

Deprecated Methods Removal

We can see that the Laravel core team is removing deprecated methods in Laravel 9 from the Laravel 10 branch. We predict the team will update the documentation’s upgrade guide to include all deprecated methods and packages as soon as Laravel 10 is released.

If you’re going to migrate a current project to Laravel 10, any code that uses a deprecated method should be re-written in a new approach to achieve the same result.

Here is a list of all the deprecations and deletions we found while comparing Laravel 9 to the master branch:

  • The Route::home method (deprecated in Laravel 9)
  • The deprecated functions and methods around dispatchNow. This is to encourage developers to use dispatchSync which is the only supported way to dispatch immediately.
  • The getBaseQuery because it has a toBase equivalent
  • The MaintenanceModeException class which was not used anymore
  • The MocksApplicationServices trait
  • The mail fake’s Mail::failures method
  • The deprecated $dates property, it’s recommended to use $casts instead
  • The assertTimesSent() method
  • Dropped support for Predis 1 and doctrine/dbal 2
  • All related deprecations in doctrine/dbal since Laravel dropping support for version 2

How To Install Laravel 10

Laravel 10 is already available now for you to take for a ride and test its features. The –dev flag in the Laravel installer installs the master branch from the laravel/laravel repository. All what you will have to do is run this command in your terminal:

laravel new example-kinsta-app --dev

Or, if you prefer using Composer:

composer create-project --prefer-dist laravel/laravel example-kinsta-app dev-master

To better understand the Composer command, here is a quick explanation:

  • laravel/laravel: The package for the Laravel installation
  • example-kinsta-app: The new directory for your new project (can be changed)
  • dev-master: The next version of Laravel (in this case, Laravel 10)

After installing Laravel 10, you can confirm the version by navigating to the new directory example-kinsta-app and run the artisan command:

$ php artisan --version
Laravel Framework 10.x-dev

How To Upgrade a Project to Laravel 10

Are you tempted to upgrade to Laravel 10? The Laravel core team works hard on documentation to provide a seamless and straightforward upgrade guide while covering every possible breaking change. Feel free to check Laravel 10 upgrade guide, as some information about the upgrade process is available now.

You should also keep an eye on Laravel Shift once Laravel 10 is released. It offers a simple and automated approach to upgrading your Laravel version.

In addition to Laravel documentation and Laravel Shift, we at Kinsta will publish a comprehensive upgrade guide with real-world examples. So, don’t forget to bookmark this page and return to it once Laravel 10 is released.

How To Deploy Laravel 10 Projects

Deploying Laravel 10 shouldn’t be so different from deploying Laravel 9 project. Here’s what we expect the server requirements might be:

  • PHP >= 8.1
  • BCMath PHP Extension
  • Ctype PHP Extension
  • cURL PHP Extension
  • DOM PHP Extension
  • Fileinfo PHP Extension
  • JSON PHP Extension
  • Mbstring PHP Extension
  • OpenSSL PHP Extension
  • PCRE PHP Extension
  • PDO PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

You may deploy and host your Laravel 10 project on Kinsta in a matter of minutes since Laravel is one of a long list of supported frameworks for hosted apps.

Deploying Laravel 10 on Kinsta: Step-by-Step Guide

There are several benefits to hosting and deploying your applications on Kinsta, including not having to bother with the deployment configuration.

Kinsta provides a user-friendly yet powerful tool for deploying applications, connecting to databases, and monitoring live performance. This tool is known as MyKinsta, and in this section, we will walk you through the steps to deploy a Laravel 10 application using it.

1. Laravel 10 Application

Laravel 10 application homepage
The homepage of a Laravel 10 application deployedon Kinsta.

Assuming that you have a Laravel 10 application locally, we need to make sure that it has a github repository available. We will use this repository to deploy the application later.

You may include an .htaccess file in the root directory of your application that contains the below:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

For Laravel, this redirects all requests to public/index.php. However, if needed, you may update this command while adding your application (Set up your processes step) or after deployment on the application’s Processes page. You can use:

  • heroku-php-apache2 /public
  • php artisan serve --host 0.0.0.0 --port 8080

2.Login to MyKinsta

MyKinsta Login Page
Screenshot for MyKinsta login page.

Head to the MyKinsta login page and enter your email address and password. If you don’t yet have an account, you can register a new account and get $20 off your first month of any pricing plan subscription.

3. Authorizing Kinsta on GitHub

Once you login, you will notice that the dashboard has 3 options to create a WordPress site, create an Application or create a database. We will press on the Connect GitHub button to integrate with GitHub.

MyKinsta dashboard
A screenshot for MyKinsta dashboard.

Next, click on the Continue with GitHub button. If you aren’t already logged in to GitHub, you’ll be shown the option to log in. Then you may authorize the Kinsta GitHub application to access your GitHub account by selecting Authorize Kinsta.

Finally, GitHub will redirect your browser to MyKinsta in order to continue setting up the application.

Connecting MyKinsta with GitHub
Screenshot for connecting MyKinsta with GitHub.

After you’re connected to GitHub, you will be shown the Add Application popup/modal, which has a dropdown menu to select a GitHub repository. Click the GitHub repository field and select Edit GitHub permissions in the dropdown.

Adding an application to MyKinsta
Screenshot for adding an application to MyKinsta.

This will open a new tab in your browser to the GitHub webpage where you can choose which data Kinsta will be able to access. Instead of enabling access to all repositories, consider choosing only the one you want Kinsta to use. Click Only select repositories, and choose the repository you would like to deploy.

Installing MyKinsta on GitHub
A screenshot for installing MyKinsta on GitHub.

Then, click Install, and you’re all set!

When you return to MyKinsta and click the GitHub repository field, the authorized repository should be visible. Additionally, you may tick Automatic deployment on commit checkbox to enable MyKinsta’s feature to auto-deploy your application as soon as you make any changes to the GitHub repository.

Selecting repository branch for MyKinsta
Screenshot for Selecting repository branch for MyKinsta.

4. Add Application Basic Details

You can now give the application a name that will only be used in the dashboard and select from 25 data center locations, allowing you to place your application in the geographical location nearest to your users. If you need assistance determining which data center is ideal for your application, check out our guide to choosing the right data center location.

Adding application basic details
Screenshot for adding application basic details.

5. Add Environment variables

The following section is for adding environment variables and the most important variable is the APP_KEY.

If you don’t have a key in your .env file already, you can generate one using an online Laravel key generator. Then, insert APP_KEY in the Key 1 field and insert the generated app key in the Value 1 field.

Finally, select Available during runtime and Available during build process.

Adding env variables to MyKinsta application
Screenshot for adding env variables to MyKinsta application.

5. Configure Build Environment

The magical thing here is that you don’t have to configure anything! You may proceed by clicking the Continue button, and voilà! You have completed the built environment configuration. However, if you want to enhance the build resources to ensure faster building, you may pick the preferred choice from the Build resources field.

The Build path field is optional — you may leave it empty and MyKinsta will use the root folder.

MyKinsta application build environment
Configuring the application’s build environment.

6. Set up the Processes and Payment Method

For the Resources section, fill in each field with the following information:

  • Process name: The name displayed in the list of your application’s processes.
  • Process type: Choose carefully, because this cannot be changed once it’s set. You can, however, add and change additional processes, like a background job, on your application’s Processes page after deployment.
  • Start command: The “Start” command for your process (not required).
  • Pod size: The pod capacity you expect you’ll need for your process.
  • Instance count: The number of instances for your process (max 50).

Note that you can simply click Continue without filling the Start command field, as Kinsta automatically detects the required command during the first deployment.

MyKinsta application setup process
Screenshot for setting up the application’s process.

For the last step, review the monthly usage costs calculated for your app, then confirm your payment method. When finished, click the Confirm payment method button.

Confirming payment details for MyKinsta
Screenshot for confirming payment details.

And you’re done! Kinsta will do all the work in the background to serve your application.

Loading application's deployment process
Screenshot for loading application’s deployment process.

Then you’ll have access to all of the deployment process details, as well as the temporary live app URL, which you can later replace with your own Domain.

Successful deployment for the application
Successful deployment of Laravel10 application on MyKinsta.

Your Laravel 10 application is now live on Kinsta, but what if you need to make a change in production? What if we want to change an anchor tag’s href attribute? Since we have activated the Automatic Deployment on Commit feature, MyKinsta will detect any changes we make to the deployed branch and automatically update the live app accordingly.

7. Connect Laravel 10 With MySQL Database

Now that we’ve deployed our Laravel 10 application, we can effortlessly construct a database and link it to our application. All you have to do is click Applications from the navigation menu on the left, then Add Service and select Database.

Adding new database to MyKinsta
Screenshot for adding new database to MyKinsta.

After filling all the fields, click Create database. This will establish a new database ready for internal and external connections. In our scenario, we require an internal connection to the Laravel 10 project that we deployed.

To achieve that, all you need to do is to click on Add application in the Internal connections section and select your application. You can check the Add environment variables to the application checkbox and MyKinsta will fill in all the .env variables your application needs.

Connecting database to Laravel 10 app
Screenshot for connecting database to Laravel 10 application.

With just that, your Laravel 10 application is deployed and connected to a database.

How To Contribute to Laravel 10

Although Laravel is maintained by a core team, it’s actively developed by over 3,000 volunteer contributors.

Do you want to be one of those contributors and help shape Laravel’s future? If you answered yes, you could help developers all over the world by adding a new feature, fixing a bug, or even rewriting a confusing part of the documentation.

To contribute to Laravel 10, here is what you need to do:

  1. Head to Laravel’s GitHub repository and check out the pull requests tagged with [10.x] in the title. This will provide you a clear picture of all the pull requests for Laravel 10. If one of the PRs addresses the contribution you intended to make, see if you can improve on it.
  2. If your planned contribution has not yet been addressed by someone else, then you may create a PR yourself.
  3. Not everything is worth adding to the framework codebase. Therefore, strive to only implement improvements that will be easy to maintain in the future and will help the vast majority of the Laravel community.
  4. Ensure adhering to Laravel’s contribution guidelines for a better chance of getting your changes merged with the framework.

Another reason to love Laravel 10 is that it allows you to win money for your contributions with bug hunts! We’ll look at those next.

Laravel 10 Bug Hunt Contest

Laravel 10 bug hunt contest
Laravel 10 bug hunt contest.

Laravel 10 has announced an excellent contest in which a random contributor has the chance to win $1,000.

This will be the first contest of its kind in Laravel history. It was designed to encourage the community to find and patch hidden bugs in Laravel 10.

The rules are straightforward:

  • Only PRs submitted to the laravel/framework repository’s 10.x branch are eligible.
  • Only “genuine” bug fixes will be considered. New features, refactoring, and typo fixes are not considered.
  • Every bug fix must be supported by a test.
  • Accepted bug fixes will be labeled on GitHub, and a random winner will be announced at the end of the contest.

The contest will finish when the first stable version of Laravel 10 is released. Any pull requests that would still be pending by that time or submitted after the release of Laravel 10 will be ineligible.

Summary

That’s not it for this article! There will be more changes up until the release date. But for the time being, Laravel 10 appears to be extremely promising, and we’re excited to cover all of the gifts it brings to the PHP world.

In the meantime, if you’re looking for more ways improve the performance of your Laravel app, you might consider switching to a premium hosting provider. Kinsta’s Application Hosting solutions are among the fastest, most secure, and easiest to use in the industry, and they’re specifically designed with developers in mind, offering production streamlining and 24/7 expert support from our own seasoned developers.

The post What’s New in Laravel 10: A Deep Dive Into the Latest Updates and Features appeared first on Kinsta®.

版权声明:
作者:zhangchen
链接:https://www.techfm.club/p/39327.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>