Laravel 5.3 预热:10 个你须要知道的变动

本文经受权转自 PHPHub 社区php

说明

Laravel 5.3 发布临近,大致构建已经完成,文档整理完成后便可发布。laravel

下面是对 Laravel 5.3 新特性的整理,不完整列表。git

一、全文搜索 Laravel Scout

Laravel Scout 是针对 Eloquent 开发的基于驱动的全文搜索方案,默认集成了对 Algolia 搜索服务 的支持,因为它是基于驱动的,你能够经过它集成任何其它搜索引擎。github

Scout 经过在已有模型上实现 "Searchable" trait 来实现搜索功能,而后只需同步数据到搜索服务便可:web

php artisan scout:import App\\Post

以后就能够经过如下方式进行搜索:api

Post::search('Alice')->get();

还能够对结果进行分页:数组

Post::search('Alice')->paginate();

甚至是支持简单的 where 条件语句:app

Post::search(‘Alice’)—>where('acount_id', '>', 1)->paginate();

二、邮件操做 Laravel Mailable

Laravel Mailable 是一个崭新的 Mail 操做类,经过一种更加优雅的方式发送邮件:oop

Mail::to('laravel@example.com')->send(new OrderComplete);

固然,还支持其余全部邮件功能:post

Mail::to('laravel@example.com')
            ->cc('john@example.com')
            ->queue(new OrderComplete);

三、消息通知系统 Laravel Notifications

Laravel Notifications 容许你经过 Slack、短信或者邮件等服务实现快速更新。

Notifications 附带了一个响应式邮件模板,通知类中惟一须要作的就是像下面这样发送消息:

$this->line('Thank you for joining')
    ->action('Button Text', 'http://url.com')
    ->line('If you have any questions please hit reply')
    ->success()

错误处理:

$this->line('Sorry we had a problem with your order')
    ->action('Button Text', 'http://url.com')
    ->error()

四、Laravel Passport

Laravel Passport 是一个可选的扩展包,提供了完整可用的 oAuth 2 服务。

你能够本身设置 scope、Vue.js 模块以便执行生成、撤回 token 等操做。

五、回溯一个迁移

新功能容许你回溯一个迁移文件,以前只能回溯 最后执行的一次 的迁移(一次有多个迁移文件)。

php artisan migrate:rollback --step=1

六、Blade 里的 $loop 变量

你能够在 foreach 循环中使用魔术变量 $loop

@if($loop->first)
    Do something on the first iteration.
@endif

@if($loop->last)
    Do something on the last iteration.
@endif

七、Eloquent firstOrCreate

例子:使用 GitHub 登陆时检查 GitHub ID 是否存在,若是不存在而且你建立了新用户的话,你想要保存用户的头像:

以前这么作:

$user = User::firstOrNew(['github_id', $githubUser->id]);

if (! $user->exists) {
    $user->fill(['avatar' => $githubUser->avatar])->save();
}

return $user;

使用 firstOrCreate:

return User::firstOrCreate(['github_id', $githubUser->id], ['avatar' => $githubUser->avatar]);

八、路由存放路径改变

以前全部路由默认存放在 app/Http/routes.php 单一文件里,如今转移到根目录 routes/ 里的 web.phpapi.php 两个文件中。

九、App 文件夹结构改变

file

十、查询语句构造器永远返回集合

以前 get 返回的是数组,之后统一返回集合:

$collection = DB::table('posts')->get();

参考

相关文章
相关标签/搜索