Laravel 5.2.23 新特性預覽

來源:互聯網
上載者:User
截至發布這篇文章的時候,Laravel 在 Github 上已經有 911 名貢獻者,他們中的一些人正在協助架構在日常的基礎上加入一些更棒的東西。下面總結了一下 Laravel 5.2.23 中將加入的一些新功能。

1. in_array() 校正規則

Laravel 中的數組校正是非常棒的,最近我把我項目中的很多代碼進行了替換,只需要簡短的幾行代碼。在 5.2.23 中將加入一個新的規則,協助校正一個 key 的值是否在另一個相關的 key 中。

Validator::make(    [        'devices' => [['user_id' => 1], ['user_id' => 2]],        'users' => [['id' => 1, ['id' => 2]]]    ],    ['devices.*.user_id' => 'in_array:users.*.id']);

這段代碼會確保 user_id 的值都在 users 中。

2. Arr::first() & Arr::last() 提供了可選的回呼函數

在前一個版本中,回呼函數作為第二個參數是必選的,而在 5.2.23 中將變成選擇性參數。

$array = [100, 200, 300];// (NEW) This will return 100Arr::first($array); /** same for **/ array_first($array);// (NEW) This will return 300Arr::last($array); /** same for **/ array_last($array);// (You still can) do this and return 200Arr::first($array, function ($key, $value) {    return $value >= 150;});

3. 一次指定多個中介軟體

當為控制器添加中介軟體的時候,現在可以在一條語句中註冊多個中介軟體了。

$this->middleware(['auth', 'subscribed'], ['only' => ['getCandy']]);

4. Blade 中添加了新的指令 @php, @endphp, @unset

@php 指令允許你這樣寫 PHP 語句:

@php($count = 1)@php(++ $count)
@php    $now = new DateTime();    $environment = isset($env) ? $env : "testing";@enphp

@unset() 其實就是對 unset() 的一個封裝:

@unset($count)

5. 重寫 Blade 核心指令的能力

在 5.2.23 之前你是不能擴充 Blade 和重寫核心指令的,而現在你可以重寫任何的核心指令。

6. 避免編譯 Blade 指令

現在你可以在 Blade 指令前添加一個 @ 符號來避免編譯指令。

// output: @continue// output: @continue@@continue

7. 對 SparkPost 新的郵件驅動

8. 新的調度命令 monthlyOn()

$schedule->call(function () {    DB::table('shopping_list')->delete();})->monthlyOn(4, '12:00');

9. 新的 app()->isLocale() 方法

// Instead of thisif (app()->getLocale() == 'en')// You can do thatif (app()->isLocale('en'))

10. 使用查詢產生器查詢 MySQL 5.7 產生 JSON 欄位更加流暢

MySQL 5.7 發布後,引入了一個新的 JSON 類型的欄位,在 Laravel 5.2.23 中你可以像之前那樣快速的查詢出 Json 欄位。

假設我們的 users 表中有個 JSON 類型的 name 列,其中有下面這樣一個值:

{"en":"name","ar":"nom"}

你可以使用下面的文法查詢該值:

User::where('name->en', 'name')->get();// You may dive deep in the JSON string using the `->` operator.User::where('contacts->phone->home', 1234);

11. 測試輔助方法 seeElement() 和 dontSeeElement()

假設有下面這樣的元素:

你可以使用下面的測試方法:

$this->seeElement('image', ['width' => 100, 'height' => 50]);$this->dontSeeElement('image', ['class' => 'video']);

12. 隱藏福利 #1

你知道你已經可以這麼做了嗎?

User::whereNameAndEmail('jon', 'jon@theWall.com')->first();User::whereNameAndEmailOrPhone('jon', 'jon@theWall.com', '123321')->first();DB::table('users')->whereEmailOrUsername('mail@mail.com', 'themsaid')->first();

13. 隱藏福利 #2

// Instead of this:if(!$item){    abort(404);}// You can do that:abort_unless($item);// You may also have something like this:abort_if($item->is_hidden);

譯自: themsaid ,轉載請註明來自Specs’ Blog。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.