What's new in Laravel 7

Auteur: Maarten Bode

With Laravel 6 (released in September 2019) came an update to the versioning scheme of Laravel, it's now following semantic versioning. Because of this change, a major release is released every six months.

Laravel 7 is packed with new features we at OrangeTalent are pretty excited about.

Route performance improvements

Because of an improvement to the Symfony router component, routes will be matched twice as fast when using route caching (php artisan route:cache).

ZTTP

Guzzle is a great HTTP client for PHP, but it can sometimes be a bit verbose. As of this release Laravel provides a thin layer on top of Guzzle to make performing requests easier and cleaner.

$response = Http::withHeaders(['user-agent' => 'OrangeBot'])->post('blog', [
    'title' => 'Laravel 7',
]);

We plan on migrating to the new HTTP client for our integrations with external services.

GitHub PR πŸ‘‰

Anonymous Components

Our Belgian friends at Spatie launched Blade X a few years ago. It allows you to use Blade components in a Vue-like style.

// Without Blade X
@include('myAlert', ['type' => 'error', 'message' => $message])

// With Blade X
<my-alert type="error" :message="$message" />

This feature has now been implemented in Laravel:

// welcome.blade.php

<x-avatar size="100" />

// components/avatar.blade.php

@props(['user', 'size'])

<img
    class="d-inline-block rounded-circle"
    src="{{ $user->gravatarUrl($size) }}"
    width="{{ $size }}"
    height="{{ $size }}"
/>

GitHub PR πŸ‘‰

Custom Casts

This allows defining your own cast classes:

class Post extends Model
{
    public $casts = [
        'name' => StringCast::class,
    ];
}

class StringCast implements CastsAttributes
{
    public function get($model, string $key, $value, array $attributes)
    {
        return str_split($value);
    }

    public function set($model, string $key, $value, array $attributes)
    {
        return implode('', $value);
    }
}

GitHub PR πŸ‘‰

Custom Stubs

When running php artisan stub:publish, a stubs folder be placed in your project, allowing you to modify the default stubs files (used when you run make:model / make:controller etc.).

GitHub PR πŸ‘‰

Airlock

Building an authentication API for PWA & mobile apps has always been a kind of a pain. Airlock provides a nice built-in system for authentication using regular Laravel sessions. This also makes using authentication in external applications easier and more secure.

Airlock documentation πŸ‘‰

Route Model Binding Improvements

It's now possible to define a binding column per route, so the model is automatically bound in the controller:

Route::get('posts/{post:slug}', function (Post $post) {
    return $post;
});

Conclusion

We can't wait to upgrade our projects to Laravel 7 in the coming months! And many more features are coming in the new Laravel 7 release. Follow the release notes to see what’s new.