Home  >  Article  >  Laravel 7 is about to be released, let’s take a look at what new features and functions it has!

Laravel 7 is about to be released, let’s take a look at what new features and functions it has!

藏色散人
藏色散人forward
2020-03-03 15:56:025454browse

Laravel 7 is about to be released, let’s take a look at what new features and functions it has!

The official release date of Laravel 7 is March 3, 2020. According to Laravel Framework's major laravel version release policy every 6 months (February and August), it is another major version. It is not an LTS release, so according to the Laravel release support policy, they provide 6 months of bug fix support until September 3, 2020, and 1 year of security issue fix support until March 3, 2021. Let’s take a look at the new features and improvements in Laravel 7. (laravel technical article)

New features of Laravel 7

● Laravel Airlock

● Zttp for HTTP client

● CORS support

● Custom Eloquent Cast

● Smooth string operation

● Blade X

● Customizable Stubs

● Query Time Conversion

● Multiple Mail Drivers

● New Artisan Commands

Improvements in Laravel 7

● Routing model binding improvements

● 2x faster routing

● Database queue improvements

● Markdown email template improvements

● And more bug fixes and improvements.

Laravel Airlock

Laravel Airlock is the official package for API authentication. It provides simple token-based API authentication, token issuance, token capabilities, mobile app authentication, and more.

Zttp for HTTP Client

Using Zttp, making HTTP requests to the API would be a better and more concise approach.

Post request

<?php
use Illuminate\Support\Facades\Http;
$response = Http::post($url);
$response = Http::post($url, [
    &#39;site&#39; => &#39;Laravel Article&#39;,
]);

Get response

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

With request header

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

Response

$response[&#39;foo&#39;]
$response->body()
$response->json()
$response->status()
$response->ok()

CORS support

Laravel 7 now supports CORS (Cross-Origin Resource Sharing) out of the box. You should better understand that every developer has encountered CORS issues in API development. Laravel 7 now automatically responds to OPTION requests using configuration values. Laravel 7 comes with HandleCors middleware out of the box that does it all.

Custom Eloquent Cast

Custom eloquent casting in Laravel 7 is another cool feature. This feature will enable you to add custom casts. Let's take a look at 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 the custom eloquent cast in the model.

<?php
namespace App;
use App\Casts\Json;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
    protected $casts = [
        &#39;extra&#39; => Json::class,
    ];
}

Fluent String Operations

In Laravel 7, you can use the Illuminate\Support\Str class to do more cool object-oriented things.

$currentVersion = (string) Str::of(&#39;  Laravel 6.x &#39;);
return $currentVersion->trim()
        ->replace(&#39;6.x&#39;, &#39;7.x&#39;)
        ->slug();
// laravel-7x

Blade X

Laravel 7 Blade X features enable you to make class-less components.

Generate 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 Usage

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

Customize Stubs

Now you can use artisan command in Custom stubs in Laravel 7.x.

php artisan stub:publish

Query Time Conversion

Laravel 7 provides the withCasts method to help you cast values ​​while running a query. Let's give an example.

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

Multiple Mail Drivers

Laravel 7 will allow you to set up multiple mail drivers using a single application.

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

New Artisan Command

A new artisan test command has been added to Laravel 7. The new artisan test command gives you a beautiful UX and useful information about your tests.

php artisan test

Improvements in Laravel 7

● Routing model binding improvements

● 2x faster routing

● Database queue improvements

● Markdown email template improvements

● and more bug fixes and improvements.

Route model binding improvements

Custom Key

By default, route model binding is used with the id field. Now you can customize it.

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

Auto-scoping

Laravel 7 will automatically scope queries to retrieve nested models using the method it identifies conventions for identifying relation calls in your program.

use App\Post;
use App\User;
Route::get(&#39;api/users/{user}/posts/{post:slug}&#39;, function (User $user, Post $post) {
    return $post;
});

2x faster routing

When using route:cache, Laravel 7’s route matching performance is 2x faster than laravel 6

Database Queue Improvements

Laravel 7 has improvements for applications that use MySQL 8 as a database-backed queue.

Markdown email template improvements

The default markdown template for emails has a newer look using the Tailwind CSS palette. Templates can be published and customized as needed.

For more programming related content, please pay attention to the Programming Introduction column on the php Chinese website!

Statement:
This article is reproduced at:NiZerin. If there is any infringement, please contact admin@php.cn delete