laravel5.2.15_深入了解Laravel 5.3功能

laravel5.2.15

Laracon US 2016会议已售罄! ( Laracon US 2016 Conference is Sold Out! )

With the 2016 Laracon US conference already sold out and just but a few weeks away, it is expected that Laravel 5.3 will be released during the 3 day conference from July 27th to July 29th of 2016.

由于2016年的Laracon美国会议已经售罄,仅几周后,预计Laravel 5.3将在2016年7月27日至7月29日的3天会议期间发布

We'll have Chris, Nick, and Mathias out at Laracon this year so look for a first-hand recap during Laracon.

克里斯尼克马蒂亚斯(Mathias)将于今年在拉腊Kong(Laracon)参加,因此希望在拉腊Kong期间进行第一手回顾。

In this article, we will take a look at what to expect in 5.3

在本文中,我们将了解5.3的期望

介绍Laravel Echo ( Introducing Laravel Echo )

In May 2016, Taylor Otwell made a screencast on Laracasts introducing Laravel Echo using Laravel Spark as one of the new things coming in Laravel 5.3.

2016年5月,Taylor Otwell在Laracasts上进行了电视广播,介绍了使用Laravel Spark作为Laravel 5.3中的新功能之一介绍Laravel Echo。

Laravel Echo is a vast improvement over the event broadcasting system In Laravel and how it Interacts with Pusher.

Laravel Echo是对Laravel中的事件广播系统及其与Pusher的交互方式的巨大改进。

-Taylor Otwell

-泰勒·奥特威尔

Laravel Echo basically makes it easier to work with web sockets by removing all the complexities involved in making real time web applications intuitive.

通过消除使实时Web应用程序变得直观的所有复杂性,Laravel Echo基本上使使用Web套接字更容易。

There is no documentation yet on Laravel Echo so we will not delve into how to create a real time app sample just yet.

Laravel Echo上没有任何文档,因此我们暂时不会深入研究如何创建实时应用示例。

For those of you that dare venture, such as Matt Stauffer that have gone ahead to dig into the source, you may tinker with Laravel Echo in one of the following ways

对于那些敢于冒险的人,例如马特·斯塔弗Matt Stauffer) ,他们已经开始深入研究源代码,您可以通过以下其中一种方式来修改Laravel Echo

  1. Watch Taylor Otwell's screencast on how to create a simple realtime todo app with Vue.js, Laravel Spark and *Laravel Echo *

    观看Taylor Otwell的截屏视频 ,其中介绍了如何使用Vue.jsLaravel Spark和* Laravel Echo *创建一个简单的实时待办应用程序
  2. Take a look at Matt Stauffer's blogpost on how get started with Laravel echo in creating a simple chat application.

    看看Matt Stauffer的博客文章 ,了解如何在创建简单的聊天应用程序时使用Laravel echo入门。

引入新的$ loop变量 ( Introducing the New $loop Variable )

Laravel's blade templating engine provides convenient directives for working with PHP's control and loop structures.

Laravel的刀片模板引擎为使用PHP的控制和循环结构提供了方便的指令。

Taking a look at the loop structures, Laravel allows us to iterate through data with ease.

看一下循环结构,Laravel允许我们轻松地遍历数据。

<ul>
@for($i = 0; $i < 10; $i++)
  <li>{{ $i }}</li>
@endfor
</ul>

@foreach($users as $user)
  <p>{{ $user->name }}</p>
@endforeach

Laravel 5.3 will be introducing a new $loop variable that will be available for each iteration in the @foreach directive.

Laravel 5.3将引入一个新的$loop变量,该变量可用于@foreach指令中的每次迭代。

The loop variable will have some of the following properties.

循环变量将具有以下某些属性。

  1. first: Boolean value that evaluates to true on the first iteration.

    first :布尔值,在第一次迭代时评估为true。
  2. last: Boolean value that evaluates to true on the last iteration.

    last :布尔值,在上一次迭代时评估为true。
  3. index: A 1-based index of the items in the loop with the first item being at index 1.

    index :循环中各项的基于1的索引,第一个项位于索引1。
  4. count: The number of items on the loop.

    count :循环中的项目数。
  5. remaining: The number of items remaining in the loop. For n items in a loop with the current position being 4, this value would be n-4

    remaining :循环中剩余的项目数。 对于当前位置为4的循环中的n个项目,该值为n-4
  6. depth: Integer value of the depth of the loop.

    depth :循环深度的整数值。
  7. parent: Returns a reference to the parent $loop for a foreach with a depth of more than 1, otherwise, null for a loop with a depth of 1.

    parent :对于深度大于1的foreach ,返回对父$loop的引用,否则对深度为1的循环返回null。

What this means is that you will be able to do cool things like:

这意味着您将能够做一些很酷的事情,例如:

在排行榜上显示玩家相对于所有玩家的位置 (Show the position of a player against all players on a leader board)
@foreach($leaderboard as $player)
  <p>{{ $player->name }} - {{ $loop->index }} of {{ $loop->count }}</p>
@endforeach
对循环中的第一项和最后一项具有特殊的样式 (Have special styling for the first and last items in a loop)
@foreach($leaderboard as $player)

  @if($loop->first)
    <p class="winner">{{ player->name }}</p>
    @continue
  endif

  @if($loop->last)
    <p class="loser">{{ player->name }}</p>
    @continue
  endif

  <p>{{ $player->name }}</p>

@endforeach

介绍新的JSON列查询和更新方法 ( Introducing The New JSON Column Query and Update Methods )

While returning data from a database model returns JSON data, this has merely been an array that can be converted to and from JSON format.

虽然从数据库模型返回数据将返回JSON数据,但这只是一个可以与JSON格式相互转换的数组。

In Laravel 5.3, you can now look forward to a new way of querying and updating data based on the JSON properties.

在Laravel 5.3中,您现在可以期待一种基于JSON属性查询和更新数据的新方法。

这对您意味着什么? (What does this mean for you?)

In this section, we will use the same example of a players table.

在本节中,我们将使用players表的相同示例。

A player can have attributes that are vital to the player character, this details which include name, date of birth, phone number and email address are justified to have their own respective fields.

玩家可以具有对玩家角色至关重要的属性,包括姓名出生日期电话号码电子邮件地址在内的这些详细信息应具有各自的字段。

We can however have some additional data that may not be key to identify the character of a player record but may be necesarry to have as part of a player's profile.

但是,我们可以拥有一些其他数据,这些数据可能不是识别玩家记录的特征的关键,但可能有必要作为玩家个人资料的一部分。

Our Player migration will look like this:

我们的Player迁移将如下所示:

Schema::create('players', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->date('dob');
    $table->string('phone_number');
    $table->string('email');
    $table->json('stats');
    $table->timestamps();
});

This means that with a record template that looks like this, we can query and update the data based on the statistics of each player using the arrow notation.

这意味着,使用如下所示的记录模板,我们可以使用箭头符号根据每个玩家的统计信息查询和更新数据。

{
  "id": 1,
  "name": "John Kariuki",
  "dob": "2016-06-07",
  "phone_number": "XXXXXXXXXX",
  "email": "player@example.com",
  "stats": {
    "goals": 20,
    "red_cards": 2,
    "yellow_cards": 3,
    "benched": false
  }
}
使用where()方法查询JSON数据 (Querying JSON data using the where() method)

To get a list of all players with 20 goals, we will make a query whose where clause has two parameters. The first being the json field name with an arrow notation followed by the property name, and the second being the value to equate to.

为了获得所有有20个进球的球员的列表,我们将查询其where子句具有两个参数的查询。 第一个是带有箭头符号的json字段名称,后跟属性名称,第二个是要等于的值。

DB::table('players')
    ->where('stats->goals', 20)
    ->get();
使用update()方法更新数据 (Updating data using the update() method)

We can also update the player details based on a value in the JSON field.

我们还可以根据JSON字段中的值更新播放器详细信息。

DB::table('players')
    ->where('stats->red_cars', '>', 3)
    ->update(['stats->benched' => true]);

As for Laravel 5.3, this feature is limited to databases that have JSON data type support such MySQL 5.7 and PostgreSQL 9.5.

对于Laravel 5.3,此功能仅限于支持JSON数据类型的数据库,例如MySQL 5.7PostgreSQL 9.5

Take a look at how Prosper Otemuyiwa, an open source envagelist, plans on taking advantage of the JSON column to index and look up data on this Facebook post.

看看开源的envagelist Prosper Otemuyiwa如何计划利用JSON列来索引和查找此Facebook帖子上的数据。

允许将其他值传递给firstOrCreate ( Allowing Additional Values To Be Passed To firstOrCreate )

The firstOrCreate method will allow you to provide additional parameters to it.

firstOrCreate方法将允许您提供其他参数

So instead of having to check if a record exists and creating it like this:

因此,不必检查记录是否存在并像这样创建它:

$site = Site::firstOrNew(['user_name', 'scotch']);

if (! $site->exists) {
    $site->fill(['website' => 'https://scotch.io'])->save();
}

return $site;

You can now do this:

您现在可以执行以下操作:

return Site::firstOrCreate(
    ['user_name', 'scotch'],
    ['website' => 'https://scotch.io']
);

In both cases, we will get a site whose name is scotch if it exists but if does not, we will create one with a username of scotch and website value of https://scotch.io.

在这两种情况下,如果存在一个名为scotch的网站,但如果不存在,我们将创建一个名称为scotch且网站值为https://scotch.io的网站。

介绍集合中的运算符:: where() ( Introducing Operators In Collection::where() )

While the where method in Laravel 5.2 allows you to filter a collection by equality of a given key/value pair, we can expect that Laravel 5.3 to have some additional operators.

虽然Laravel 5.2中的where方法允许您通过给定键/值对的相等性来过滤集合,但我们可以期望Laravel 5.3包含一些其他运算符。

$players = collect([
    ['name' => 'John', 'age' => 20],
    ['name' => 'Jane', 'age' => 21],
    ['name' => 'June', 'age' => 22],
    ['name' => 'Alex', 'age' => 23],
    ['name' => 'Jude', 'age' => 24],
    ['name' => 'Luke', 'age' => '24'],
]);

$players->where('age', 23);
///['name' => 'Alex', 'age' => 23]

$players->where('age', '==', 24);
/*
    ['name' => 'Jude', 'age' => 24],
    ['name' => 'Luke', 'age' => '24']
 */

$players->where('age', '===', 24);
//['name' => 'Jude', 'age' => 24]

$players->where('age', '<=', 22);
/*
    ['name' => 'John', 'age' => 20],
    ['name' => 'Jane', 'age' => 21],
    ['name' => 'June', 'age' => 22]
 */

With the operator parameter, you have the option of using one of the following:

使用operator参数,您可以选择使用以下选项之一:

= == === != !== <= >= < > <>

= == === != !== <= >= < > <>

This in turn means that the whereloose method will be deprecated since there will be no need for loose comparisons.

反过来,这意味着不推荐使用whereloose方法,因为不需要进行松散的比较。

查询生成器返回一个集合 ( Query Builder Returns A Collection )

In Laravel 5.2, the database query builder returns an array where each result is an instance of the PHP StdClass object.

在Laravel 5.2中, 数据库查询构建器返回一个数组,其中每个结果都是PHP StdClass对象的实例。

This is bound to change in Laravel 5.3 to return a collection instead.

在Laravel 5.3中,这势必会更改,以改为返回一个集合。

I personally think will be a really good change since you do not need the collect method to convert the array to a Laravel collection. It also goes without saying that collections are much easier to work with as opposed to arrays.

我个人认为这将是一个非常不错的更改,因为您不需要collect方法将数组转换为Laravel集合。 不用说, 集合比数组更容易使用

快速了解迁移 ( A Quick Look At Migrations )

介绍Laravel迁移路径 (Introducing Laravel Migration Paths)

Laravel 5.3 will support multiple migrations paths through a service provider.

Laravel 5.3将支持通过服务提供商的多个迁移路径。

$this->loadMigrationsFrom('path/to/your/migrations/folder')

With this, running migrations from your Laravel project and all the packages that need to run migrations is as easy as php artisan migrate

这样,从Laravel项目中运行迁移以及运行迁移所需的所有程序包就像php artisan migrate migration一样容易

While this may not seem as a big improvement compared to some of the other improvements, migration paths are bound to make it easier to load migrations from packages.

尽管与其他一些改进相比,这似乎不是一个很大的改进,但迁移路径必将使从程序包装载迁移更加容易。

According to Alex Bowers in this article,

根据Alex Bowers在本文中的介绍

A required step previously was to have a publication step, or a copy paste step for the migrations from vendor/package/database/migrations to database/migrations.

以前需要执行的步骤是发布步骤或复制粘贴步骤,以便从供应商/包/数据库/迁移数据库/迁移的迁移

This new feature allows the migrations to stay where they are, and not clutter up your migrations folder.

这项新功能使迁移可以保持原样,而不会使迁移文件夹混乱。

回滚迁移,一次仅一步 (Rollback Back Migrations, One Step At A Time)

Laravel 5.3 is set to provide a new option to enable you to rollback one migration as opposed to the entire migration

Laravel 5.3设置为提供一个新选项,使您可以回滚一个迁移而不是整个迁移

php artisan migrate:rollback --step=1

存储上传的文件从未如此简单 ( Storing Uploaded Files Has Never Been Easier )

Laravel makes it all too easy to store uploaded files to any of your disks as configured in config/filesystems.php. In this case, disks refer to your storage location which currently include:

Laravel使在config/filesystems.phpconfig/filesystems.php将上传的文件存储到任何磁盘上变得非常容易。 在这种情况下,磁盘是指您的存储位置,当前包括:

  1. Local starage

    当地饥饿
  2. Amazon s3 - Implementation available through the league/flysystem-aws-s3-v3 package

    Amazon s3-通过League / flysystem-aws-s3-v3软件包可实现
  3. Rackspace - Implementation available through the league/flysystem-rackspace package

    Rackspace-通过League / flysystem-rackspace软件包提供的实现

Read more about the specific configuration for each storage drive on the filesystems documentation.

文件系统文档中阅读有关每个存储驱动器的特定配置的更多信息。

In Laravel 5.3, the UploadedFile class has a store method that accepts the file path to store the file and an optional second parameter of the storage location of choice.

在Laravel 5.3中, UploadedFile类具有一个store方法,该方法接受用于存储文件的文件路径以及所选存储位置的可选第二个参数。

$storagePath = $request->avatar->store('images');
$storagePath = $request->avatar->store('images', 's3');

The file name is intentionally omitted since an md5 hash of the file contents is generated as the file name.

由于会生成文件内容的md5哈希作为文件名,因此有意省略了文件名。

To override this implementation and store a file with a name of your choice, use the storeAs method which accepts the file name as a second parameter

要覆盖此实现并使用您选择的名称存储文件,请使用storeAs方法,该方法接受文件名作为第二个参数

$storagePath = $request->avatar->storeAs('images', 'avatar.jpg');
$storagePath = $request->avatar->storeAs('images', 'avatar.jpg', 's3');

Twitter上的单词:简单的分页模板,Laravel队列以及一些 ( Word On Twitter: Simple Pagination Templates, Laravel Queues and Then Some )

Taylor Otwell recently tweeted about Documentation driven development and if you follow him, well then let's just say you will always be up to date on peek previews of what's coming.

泰勒·奥特威尔(Taylor Otwell)最近在推特上发表了有关Documentation driven developmentDocumentation driven development ,如果您关注他,那么,那么我们可以说,您始终可以随时了解最新内容。

He wrote a tweet on June 1st 2016 on customizing pagination using views which was in response to this issue on Github. This makes it easier to have custom pagination styles as opposed to being limited to the Bootstrap template implementation.

他于2016年6月1日写了一条有关使用视图自定义分页的推文,以回应Github上的此问题 。 相对于限于Bootstrap模板实现,这使具有自定义的分页样式更加容易。

Today in Laravel 5.3 development. Bringing back simple pagination customization via views.

Some improvements in the Laravel queue service have been mentioned on twitter by Taylor Otwell and @laravelphp.

Taylor Otwell和@laravelphp在Twitter上提到了Laravel队列服务的一些改进。

Cleaning Laravel queue console output so it shows actual class names instead of CallQueuedHandler

In 5.3, Eloquent collections are cleanly serialized and re-pulled by queued jobs

结论 ( Conclusion )

I have just but lightly touched on what to look out for on Laravel 5.3 which is expected to support PHP 5.6, PHP 7 and HHVM.

我只是略微触及了在Laravel 5.3上寻找的内容,该版本有望支持PHP 5.6,PHP 7和HHVM。

While I have not touched on the methods that are being done away with besides whereloose(), I would like to mention the lists() method which will be removed in favour of the pluck() method.

尽管除了whereloose()之外,我还没有涉及其他方法,但我想提一提lists()方法,该方法将被删除,而使用pluck()方法。

Some changes are also expected in the Validation class. What changes have you seen in Laravel 5.3 so far? What are you looking forward to in 5.3? Let's get talking on the comment section.

在Validation类中还需要进行一些更改。 到目前为止,您在Laravel 5.3中看到了哪些变化? 您在5.3中期待什么? 让我们谈谈评论部分。

翻译自: https://scotch.io/tutorials/a-deep-dive-into-laravel-5-3-features

laravel5.2.15

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值