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();

參考

相關文章
相關標籤/搜索