Release Notes
- Laravel 5.8
- ##Eloquent HasOneThrough Relationship
- Automatic lookup model strategy
- PSR-16 cache specification
- Token Guard token hash algorithm
- Improved Email validator
- Default scheduled task time zone
- Intermediate table/pivot model event
- Artisan call improvement
- Mock/Spy test auxiliary method
- Eloquent resource key persistence
- Higher-order orWhere Eloquent method
- Artisan Serve improvements
- Template File Mapping
- DynamoDB Cache/Session Driver
- ##Carbon 2.0 Support
- Pheanstalk 4.0 support
Release Notes
- Version Control Scheme
- Support Strategy
- Laravel 5.8
Version Control Scheme
Laravel's version control scheme uses the following convention:
Major version number.Minor version number.Revision number
. Minor version frameworks are released every six months (February and August), while revisions may be released weekly, and revision does not contain breaking changes.When you reference the Laravel framework or other components from your application or within a package, you should always use version constraints, such as
5.7.*
, because minor versions of Laravel contain breaking changes . However, we will work hard to ensure that you can update in a day or less.Releases between major versions often take many years, and each release represents a fundamental change in the framework architecture and underlying structure. There are currently no plans to develop a major version number.
Support Policy
For LTS releases, such as 5.5, two years of bug fixes and three years of security fixes are provided. These versions provide the longest time support and maintenance. For the general version, only six months of bug fixes and one year of security fixes are provided.
Version Release Time Bug Fix Deadline Security Fix Deadline 5.0 February 4, 2015 August 4, 2015 February 4, 2016 5.1 (LTS) June 9, 2015 June 9, 2017 June 9, 2018 Day 5.2 December 21, 2015 June 21, 2016 December 21, 2016 5.3 August 23, 2016 September 23, 2017 August 23, 2017 5.4 January 24, 2017 July 24, 2017 January 24, 2018 5.5 (LTS) August 30, 2017 August 30, 2019 August 30, 2020 5.6 February 7, 2018 August 7, 2018 February 7, 2019 5.7 September 4, 2018 March 4, 2019 September 4, 2019 5.8 February 26, 2019 August 26, 2019 February 26, 2020 Laravel 5.8
Laravel 5.8 continues to optimize on the basis of Laravel 5.7, including the introduction of new Eloquent relationships (has-one-through), optimization of email verification, and automatic registration of authorization policy classes based on conventions , DynamoDB cache and Session driver, optimize the time zone configuration of the task scheduler, support assigning multiple authentication guards to broadcast channels, PSR-16 cache driver specification, optimize the
artisan serve
command, support PHPUnit 8.0, support Carbon 2.0 , supports Pheanstalk 4.0, and multiple bug fixes and usability improvements.Eloquent
HasOneThrough
AssociationsEloquent now provides support for the
hasOneThrough
association type. For example, assuming that there is a one-to-one relationship between the Supplier model class and the Account model class, and there is also a one-to-one relationship between the Account model class and the AccountHistory model class, then the Supplier model class and the AccountHistory model class can be connected throughhasOneThrough
The method establishes a one-to-one association at the remote level based on the Account model class. You can access the AccountHistory model class through the Account model class using thehasOneThrough
association:/** * Get the account history for the supplier. */ public function accountHistory() { return $this->hasOneThrough(AccountHistory::class, Account::class); }
Automatically find the model strategy
When using Laravel 5.7, the authentication corresponding to each model Strategies need to be explicitly registered in the application's
AuthServiceProvider
:/** * 当前应用的策略对应关系 * * @var array */ protected $policies = [ 'App\User' => 'App\Policies\UserPolicy', ];
Laravel 5.8 introduces automatic search for model strategies, as long as the naming of the model and strategy conforms to Laravel's standard conventions. That is, the policy must be under the
Policies
path, which contains the model. For example, the model may be placed under the pathapp
, and the policy may be placed under the pathapp/Policies
. Additionally, the policy name must match the model name and be suffixed withPolicy
. In this way, theUser
model will correspond to theUserPolicy
class.If you want to provide your own policy finding logic, you can use the
Gate::guessPolicyNamesUsing
method to register a custom callback function. Generally speaking, this method should be called from your application'sAuthServiceProvider
:use Illuminate\Support\Facades\Gate; Gate::guessPolicyNamesUsing(function ($modelClass) { // return policy class name... });
{note} Anything that is explicitly mapped to
AuthServiceProvider
strategies will take precedence over the implicit automatic search strategy.PSR-16 Caching Specification
To allow for more granular expiration times when storing cache items and comply with the PSR-16 caching standard, we changed the cache item expiration unit from minutes Adjust to seconds.
Illuminate\Cache\Repository
andput
,putMany
,add
,remember
and ## of its extension classes The validity period units corresponding to the #setDefaultCacheTimemethod and the
putmethod of each cache storage implementation class have been adjusted in this way. You can view the related PR for more details.
If the above method is called in your code, you need to update the corresponding code to ensure that the validity period passed now is consistent with the previous one (the unit becomes seconds instead of the previous minutes). As an alternative, you can also Pass a
DateTime
instance to identify the cache item's expiration time:// Laravel 5.7 - 缓存30分钟... Cache::put('foo', 'bar', 30); // Laravel 5.8 - 缓存30秒... Cache::put('foo', 'bar', 30); // Laravel 5.7 / 5.8 - 缓存30秒... Cache::put('foo', 'bar', now()->addSeconds(30));
Multiple Broadcast Authentication Watchers
In previous releases of Laravel, private and present broadcast channels Authenticate users through the application's default authentication guard. Starting from Laravel 5.8, you can assign multiple guards to authenticate requests:
Broadcast::channel('channel', function() { // ... }, ['guards' => ['web', 'admin']])
Token Guard Token hash algorithm
Laravel provides basic API authentication
token
guard now supports storing API tokens with the SHA-256 hash algorithm. This is more secure than storing plain text tokens. To learn more details about hash tokens, check out the full API Authentication documentation.Note: Even though Laravel provides a simple, token-based authentication guard, considering its robustness and providing API authentication for online applications, we strongly recommend it You use Laravel Passport.
Improved Email validator
Laravel 5.8 adopts SwiftMailer’s
egulias/email-validator
package to improve the verification logic of the email validator. Email addresses that were previously considered valid by Laravel Email validation logic, such asexample@bär.se
, may now be deemed invalid.Default scheduled task time zone
Laravel allows users to use the
timezone
method to customize the time zone of scheduled tasks:$schedule->command('inspire') ->hourly() ->timezone('America/Chicago');
If you set the time zone for each scheduled task Defining a time zone would be more cumbersome and troublesome. A simpler method is to define a
schedule Timezone
method in theapp/Console/Kernel.php
file. This method will return the default time zone to all scheduled tasks:/** * 获取默认定时任务时区。 * * @return \DateTimeZone|string|null */ protected function scheduleTimezone() { return 'America/Chicago'; }
Intermediate table/pivot model event
In previous versions of Laravel, when attaching, detaching or with a custom intermediate table / Eloquent model events are not dispatched when using a "pivot" model with a many-to-many relationship. Now in Laravel 5.8, when using the custom intermediate table model, these events will be dispatched.
Artisan call improvements
Laravel allows you to call Artisan through the
Artisan::call
method. In previous releases of Laravel, the options for a command were passed to the method via an array as the second argument:use Illuminate\Support\Facades\Artisan; Artisan::call('migrate:install', ['database' => 'foo']);
However, Laravel 5.8 allows you to pass the complete command, including its options. It will be passed as the first string parameter to the method:
Artisan::call('migrate:install --database=foo');
Mock/Spy Test Helper Method
To create mock objects more conveniently, the new
mock
and # The ##spymethod has been added to the basic Laravel test case. These methods automatically bind the mock class to the container. For example:
// Laravel 5.7 $this->instance(Service::class, Mockery::mock(Service::class, function ($mock) { $mock->shouldReceive('process')->once(); })); // Laravel 5.8 $this->mock(Service::class, function ($mock) { $mock->shouldReceive('process')->once(); });
Eloquent resource keys persistWhen an Eloquent resource collection is returned from a route, Laravel resets the collection's keys so that they are in simple numerical order:use App\User; use App\Http\Resources\User as UserResource; Route::get('/user', function () { return UserResource::collection(User::all()); });
When using Laravel 5.8, you can add a
preserveKeys
attribute to a resource class to indicate whether the resource class's keys are preserved. By default, to be consistent with previous versions of Laravel, these keys are reset:<?php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; class User extends JsonResource { /** * Indicates if the resource's collection keys should be preserved. * * @var bool */ public $preserveKeys = true; }
When the property value of
preserveKeys
is set totrue
, the collection Keys will be retained:use App\User; use App\Http\Resources\User as UserResource; Route::get('/user', function () { return UserResource::collection(User::all()->keyBy->id); });
High-level
orWhere
Eloquent methodIn the previously released Laravel, multiple queries are merged through the
or
query operator Each Eloquent model scope must use closure callbacks:// scopePopular 和 scopeActive 方法定义在 User 模型中... $users = App\User::popular()->orWhere(function (Builder $query) { $query->active(); })->get();
Lavavel 5.8 introduces the "high-level"
orWhere
method, which allows you to achieve smooth chaining of scopes without closures. style call.$users = App\User::popular()->orWhere->active()->get();
Artisan Serve improvements
In the previously released Laravel, Artisan's
serve
method will start your application service and listen on the8000
port. If aserve
command process has already been started and occupies this port, then trying to start the second application service through theserve
command will fail. Starting from Laravel 5.8,serve
will scan available ports until8009
, allowing you to start multiple application services at the same time.Template File Mapping
When compiling a Blade template, Laravel now adds a comment at the top of the compiled file containing the original Blade template path.
DynamoDB Cache/Session Driver
Laravel 5.8 introduces DynamoDB cache and session driver. DynamoDB is a serverless NoSQL database provided by Amazon Web Services. The default
dynamodb
cache driver configuration can be found in the Laravel 5.8 cache configuration file.Carbon 2.0 support
Laravel 5.8 provides support for the
~2.0
release of the Carbon date processing library.Pheanstalk 4.0 Support
Laravel 5.8 provides support for the
~4.0
release of the Pheanstalk queue library. If your application is using the Pheanstalk library, please upgrade the library to the~4.0
release through Composer.This article was first published on the LearnKu.com website.
- Multiple Broadcast Authentication Watchers