What's new in Laravel 6? Features, benefits, and serverless | Clickittech

What’s new in Laravel 6? Features, benefits, and serverless introduction

Laravel 6

Since the development of Laravel began the main team and the community of this framework has focused on providing a number of features that are used day-by-day in many websites around the world. The most recent update is Laravel 6, which has brought remarkable improvements that will be reflected in the performance of the application and during the process of debugging and developing it. 

Through this article, you’ll be able to identify the big benefits over the new features of Laravel 6, and its introduction to the serverless world. 

  1. Eloquent and the improved subqueries
  2. Laravel UI
  3. Job Middleware
  4. Lazy Collections
  5. Ignition for Developers
  6. Vapor

1. Eloquent and the improved subqueries

Laravel has its own Object Relational Model (ORM), Eloquent, to make queries to the database and which has been improved in each version. Eloquent has received an improvement in the use of subqueries that are quite common in a robust application.

Since the use of subqueries is usually a common task, it is also common to find queries made with Eloquent mixed with QueryBuilder and although this update does not intend to avoid this mixture –that is often necessary– it intends to provide the possibility of creating much more readable subqueries through the use of Eloquent models.

// Laravel 5.8
$user = User::select(['id','name', 'last_login_at' => function($query) {
         $query->select('created_at')
               ->from('logins')
               ->whereColumn('user_id', 'users.id');
     }])
     ->orderBy('name')
     ->limit(1)
     ->get();


// Laravel 6
$lastLogin = Login::select('created_at')->whereColumn('user_id', 'users.id');
$user = User::addSelect(['id','name', 'last_login_at' => $lastLogin])
            ->orderBy('name')
            ->limit(1)
            ->get();

2. Laravel UI

This version goes a step further on the user interface! In previous versions, the front-end presets were introduced through a simple command and allowed the installation of the necessary dependencies to work with bootstrap, vue.js or react.js. Without doubts, this was a great success since it encouraged the use of reactive and reusable components, however, Laravel 6 shows us that it is a much more agnostic framework. It has level independence from the much higher front-end in which development is easier to maintain and even to migrate the front-end from one technology to another without worrying about coupling to the framework.

On the other hand, the option to generate the authentication system (login, register, and password reset) has been extracted and packaged together with the front-end presets.

The installation of this package can be done as follows:

composer require laravel/ui

# front-end presets
php artisan ui react 
# or
php artisan ui vue

# with authentication views  
php artisan ui vue --auth

3. Job Middleware

One of the most fundamental tasks in a web application is the processing of background tasks, Laravel has provided this feature for a long time and a small update has arrived to keep our application code easier, and readable to perform validations before the execution of a job. Job middleware allows us to extract the logic of the handle method and put in a dedicated file, accomplishing with the Single Responsibility Principle of SOLID. 

4. Lazy Collections

In terms of PHP performance, it has always deal with the exhaustive use of memory, this is a fairly common problem that is usually solved by increasing the memory of our server, and therefore companies have a cost increment. Lazy collections allow the processing of large amounts of data in batches, which ensures that there will be no memory depletion. This characteristic combined with the jobs can maintain reasonable costs and quite good performance on your application.

Suppose we have a database with millions of users and one of the system tasks is to generate a report of all of them sorted alphabetically, traditionally it would be like this:

 $users = User::select('id', 'name')
               ->orderBy('name')
               ->all();
 // do something

Given the previous query in an ideal world, the result would be satisfactory, but due to the execution conditions, we would get the error:

Fatal error: the Allowed memory size of xxxxxx bytes 
exhausted (tried to allocate xxxx bytes).

To solve this, the old school response would be something like:

  • Edit the php.ini file and increase the memory.
  • Before execution call the function
ini_set ('memory_limit', '-1');

Both are valid answers, but… (there’s always a but) not the ideal ones. Using the new Lazy Collections, you can now perform large data queries at a low cost of memory, for example, using something like this:

$users = User::select('id', 'name')
              ->orderBy('name')
              ->cursor();
    
$users->each(function($user) {
        // do something
});

5. Ignition for Developers

Whoops was something new in Laravel that greatly helped developers to debug their applications; Ignition, on the other hand, was designed specifically to work with Laravel and aims to make debugging even simpler, since it has a wider error tracking and can even make suggestions on how to solve them by making the debugging process more efficient… How efficient? Well, Ignition shows the errors of the blade views in the blade views hehe, and not in the compiled views as its predecessor (Whoops) does, no doubt this feature will be liked by the entire developer community.

6. Vapor

As I mentioned above, the presentation of Laravel 6 brought with it some very interesting news, and one of the most interesting and important is Vapor. It is the new member of the Laravel ecosystem, this is a similar tool to Forge since its purpose is also focused on the administration and deployment of applications with a notable difference… the Serverless approach.

While Forge requires a manual administration of all the services and servers –to deploy, plan, scale, manage the database and SSL certificates– Vapor, for its part, takes care of everything automatically.

The development of Vapor took almost a year, a titanic effort by its creator Taylor Otwell, who has put Laravel and PHP into the world of Serverless. All this work resulted in this service which is responsible for configuring all the services that our application needs and the unique requirement is to have an account on Vapor and linking it with an AWS account. 

It is important to mention that all of this is possible thanks to AWS, being Serverless and having Amazon support. All we need is to focus on the development of the application since Vapor will configure for us, databases (MySQL, Aurora, PostgreSQL and others coming soon), queues (Redis), jobs, load balancers, SSL certificates, DNS, and storage(S3), among other services.

To make our traditional Laravel application works as a serverless application it is not mandatory to use the 6th version, although it is recommended, we only have to install the Vapor package in our application using the following command:

#Vapor CLI is not mandatory but is recommended too

composer require laravel/vapor-core 

With the arrival of Laravel Vapor, advanced technical knowledge of the infrastructure may not be as necessary as the dashboard interface, since it is quite simple and friendly, a System Administrator is not essential any more and this means costs reductions.

To sum up

After reviewing the benefits of Laravel 6, it is clear that migrating or creating an application using this version is a good decision since it is as robust and efficient as always. And of course, implementing Vapor is even a better option due to the low cost that represents the use of the serverless approach, in addition to having all the support of the robust AWS infrastructure.