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, [ 'site' => 'Laravel Article', ]);
Get response
$response = Http::get($url); $response = Http::get($url,['foo'=>'bar']);
With request header
$response = Http::withHeaders(['foo' => 'bar'])->post($url, [ 'baz' => 'qux', ]);
Response
$response['foo'] $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 = [ 'extra' => 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(' Laravel 6.x '); return $currentVersion->trim() ->replace('6.x', '7.x') ->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([ '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 drivers using a single application.
Mail::mailer('noreply') ->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('posts/{post:slug}', 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('api/users/{user}/posts/{post:slug}', 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!