What's new in Laravel 7

What's new in Laravel 7

Laravel 7 has an official release date on 3rd March 2020. It is another major version according to every 6-month major laravel version release strategy (February and August) of Laravel Framework. It's not an LTS version so that according to Laravel version support policy they provide 6-month bug fix until September 3rd, 2020 and 1-year security issues fix support until March 3rd, 2020. Let's take a look at what's new and improvements in Laravel 7.

 

New in Laravel 7

  • Laravel Sanctum 
  • Zttp for HTTP client
  • CORS Support
  • Custom Eloquent Cast
  • Fluent String Operations
  • Blade X
  • Customizable Stubs
  • Query Time Casts
  • Multiple Mail Driver
  • New Artisan Command

 

Improvements in Laravel 7

  • Route Model Binding Improvements
  • 2x Faster Route
  • Database queue improvements
  • Markdown Mail Template Improvements
  • and more variety of bug fixes and improvements.

 

Laravel Sanctum 

Laravel Sanctum  is an official package for API authentication. It provides Simple token base API auth, Token issuing, Token Abilities, Authentication for Mobile application and more. You can learn more about reading the post about Laravel Sanctum.

 

Zttp for HTTP client

With Zttp, it'll be nicer & cleaner way to make an HTTP request to an API endpoint.

 

Post Request

<?php
use Illuminate\Support\Facades\Http;

$response = Http::post($url);

$response = Http::post($url, [
    'site' => 'Laravel Article',
]);

Get Request

$response = Http::get($url);
$response = Http::get($url,['foo'=>'bar']);

With header

$response = Http::withHeaders(['foo' => 'bar'])->post($url, [
    'baz' => 'qux',
]);

Responses

$response['foo']
$response->body()
$response->json()
$response->status()
$response->ok()

 

CORS Support

Now Laravel 7 supports CORS (Cross-Origin Resource Sharing) out of the box. You know better every developer face CORS problem during API development. Now Laravel 7 automatically response your OPTION request with your configured value. HandleCors middleware will take care of all, which Laravel 7 provides out of the box.

 

Custom Eloquent Cast

Custom eloquent casting in Laravel 7 is another cool features. This features will give you the ability to add your won custom casts. Let's take a look for a JSON caster.

<?php

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class Json implements CastsAttributes
{
    public function get($model, $key, $value, $attributes)
    {
        return json_decode($value, true);
    }

    public function set($model, $key, $value, $attributes)
    {
        return json_encode($value);
    }
}

Now we can use our custom eloquent cast in our model.

<?php

namespace App;

use App\Casts\Json;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    
    protected $casts = [
        'extra' => Json::class,
    ];
}

 

Fluent String Operations

In Laravel 7, you can do more cool and more object-oriented stuff with Illuminate\Support\Str class.

$currentVersion = (string) Str::of('  Laravel 6.x ');
return $currentVersion->trim()
        ->replace('6.x', '7.x')
        ->slug();

// laravel-7x

 

Blade X

Laravel 7 Blade X features give you the ability to make a class-less component.

Making x-component

@php($user = $user ?? Auth::user())
@php($size = $size ?? 50)

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

Blade x Usages

<x-avatar/>
<x-avatar size="40" />
<x-avatar size="100" />

 

Customizable Stubs

Now you can customize stubs in Laravel 7.x using artisan command.

php artisan stub:publish

 

Query Time Casts

Laravel 7 provides withCasts method that will help you to cast a value when running a query. Let's take an example.

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

 

Multiple Mail Drivers

Laravel 7 will allow you to set up multiple mail driver with a single application.

Mail::mailer('noreply')
        ->to($request->user())
        ->send(new PostUpdated($post));

 

New Artisan Command

A new test artisan command added in Laravel 7. The new test artisan command provides you with beautiful UX and useful information about your test.

php artisan test

 

Improvements in Laravel 7

  • Route Model Binding Improvements
  • 2x Faster Route
  • Database queue improvements
  • Markdown Mail Template Improvements
  • and more variety of bug fixes and improvements.

 

Route Model Binding Improvements

Key Customize

By default, route model binding works with id field. Now you can customize it.

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

Automatic Scope

Laravel 7 will automatically scope the query to retrieve the nested model by using its discern the usage of conventions to bet the relationship call on the figure.

use App\Post;
use App\User;

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

 

2x Faster Route

Laravel 7 will give you 2x faster route matching performance than laravel 6 when using route:cache

 

Database queue improvements

Laravel 7 Laravel 7 provides improvements to applications using MySQL 8+ as their database-backed queue.

 

Markdown Mail Template Improvements

The default markdown template for Mail has been more fresh look with Tailwind CSS color palette. The template can be published and customize as you wish.

 


Share

Related Post


Laravel Simple Filemanager

Speed Up Laravel website - Step by Step

Repository Design Pattern in Laravel

Laravel desktop application using Electron Js

Top 10 question-answer for New Laravel developer

Laravel Excel import export