Technology Blog

Look deep into latest news and innovations happening in the Tech industry with our highly informational blog.

What’s new in Laravel 7

hkis

On release of Laravel 7, in our previous post we covered some of the fantastic features such as Laravel Airlock, Custom Eloquent Casts and few more. This post covers the rest of them.

Laravel7-updates-banner

Multiple Mail Drivers

Laravel 7 allows the configuration of multiple “mailers” for a single application. Each mailer configured within the mail configuration file may have its own options and even its own unique “transport”, allowing your application to use different email services to send certain email messages. For example, your application might use Postmark to send transactional mail while using Amazon SES to send bulk mail.

Mail::mailer('postmark')
        ->to($request->user())
        ->send(new OrderShipped($order));

By default, Laravel will use the mailer configured as the default mailer in your mail configuration file. However, you may use the mailer method to send a message using a specific mailer configuration:

Route Caching Speed Improvements

Laravel 7 includes a new method of matching compiled, cached routes that have been cached using the route:cache Artisan command. On large applications (for example, applications with 800 or more routes), these improvements can result in a 2x speed improvement in requests per second on a simple “Hello World” benchmark. No changes to your application are required.

CORS Support

Laravel 7 includes first-party support for configuring Cross-Origin Resource Sharing (CORS) OPTIONS request responses by integrating the popular Laravel CORS package written by Barry vd. Heuvel. A new cors configuration is included in the default Laravel application skeleton.

Query Time Casts

Sometimes you may need to apply casts while executing a query, such as when selecting a raw value from a table. For example, consider the following query:

use App\Post;
use App\User;

$users = User::select([
    'users.*',
    'last_posted_at' => Post::selectRaw('MAX(created_at)')
            ->whereColumn('user_id', 'users.id')
])->get();

The last_posted_at attribute on the results of this query will be a raw string. It would be convenient if we could apply a date cast to this attribute when executing the query. To accomplish this, we may use the withCasts method provided by Laravel 7:

$users = User::select([
    'users.*',
    'last_posted_at' => Post::selectRaw('MAX(created_at)')
            ->whereColumn('user_id', 'users.id')
])->withCasts([
    'last_posted_at' => 'date'
])->get();

MySQL 8+ Database Queue Improvements

In previous releases of Laravel, the database queue was not considered robust enough for production usage, due to deadlocks. However, Laravel 7 provides improvements to applications using MySQL 8+ as their database backed queue. By using the FOR UPDATE SKIP LOCKED clause and other SQL enhancements, the database driver may now safely be used in higher volume production applications.

Artisan test Command

The test command was contributed by Nuno Maduro.

In addition to the phpunit command, you may now use the test Artisan command to run your tests. The Artisan test runner provides beautiful console UX and more information regarding the test that is currently running. In addition, the runner will automatically stop on the first test failure:

php artisan test
php-artisan-laravel-7

pic courtesy: res.cloudinary.com

Any arguments that can be passed to the phpunit command may also be passed to the Artisan test command:

php artisan test --group=feature

Markdown Mail Template Improvements

The default Markdown mail template has received a fresh, more modern design based on the Tailwind CSS color palette. Of course, this template can be published and customized according to your application’s needs:

mail-template-laravel-7

pic courtesy: res.cloudinary.com

Stub Customization

The Artisan console’s make commands are used to create a variety of classes, such as controllers, jobs, migrations, and tests. These classes are generated using “stub” files that are populated with values based on your input. However, you may sometimes wish to make small changes to files generated by Artisan. To accomplish this, Laravel 7 provides the stub:publish command to publish the most common stubs for customization:

php artisan stub:publish

The published stubs will be located within a stubs directory in the root of your application. Any changes you make to these stubs will be reflected when you generate their corresponding classes using Artisan make commands.

Queue maxExceptions Configuration

Sometimes you may wish to specify that a job may be attempted many times, but should fail if the retries are triggered by a given number of exceptions. In Laravel 7, you may define a maxExceptions property on your job class:

<?php

namespace App\Jobs;

class ProcessPodcast implements ShouldQueue
{
    /**
     * The number of times the job may be attempted.
     *
     * @var int
     */
    public $tries = 25;

    /**
     * The maximum number of exceptions to allow before failing.
     *
     * @var int
     */
    public $maxExceptions = 3;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Redis::throttle('key')->allow(10)->every(60)->then(function () {
            // Lock obtained, process the podcast...
        }, function () {
            // Unable to obtain lock...
            return $this->release(10);
        });
    }
}

In this example, the job is released for ten seconds if the application is unable to obtain a Redis lock and will continue to be retried up to 25 times. However, the job will fail if three unhandled exceptions are thrown by the job.