LoginSignup
34
33

More than 5 years have passed since last update.

Laravel 5.4 変更点のメモ

Last updated at Posted at 2017-01-25

5.4 リリースおめでとうございます。

see: https://laravel.com/docs/5.4/releases

なお、いつものことのように後半は力尽きていってます。

Markdown based email and notification

see: https://laravel.com/docs/5.4/mail#markdown-mailables

Markdownが標準で使えるようになりました。ボタン装飾などは @component を使うみたいです。

sample:

@component('mail::message')
# Order Shipped

Your order has been shipped!

@component('mail::button', ['url' => $url])
View Order
@endcomponent

Next Steps:

- Track Your Order On Our Website
- Pre-Sign For Delivery

Thanks,<br>
{{ config('app.name') }}
@endcomponent

Laravel Dusk

簡単に、そして自動的に、ブラウザ経由のテストAPIです。

SeleniumやJDKは利用せず、ChromeDriverを使います。

ブラウザ経由のテストなので、Javascriptの実行テストもできます。

Laravel Mix

Laravel Elixir の後継です。

Blade Components & Slots

Blade コンポーネントの中で、任意に変数を使うような感じっぽい。

例えば、アラート部分を分離している場合、こんな感じに分けているとする。

// alert.blade.php
<div class="alert">
    {{ $slot }}
</div>

そして呼び出し元はこのように呼び出せば良いかんじっぽい。

@component('inc.alert')
    <label>Alert</label>
    <div>This is the alert message here.</div>
@endcomponent

Broadcast Model Binding

Broadcast のルーティングが route model binding に対応しました。

use App\Order;

Broadcast::channel('order.{order}', function ($user, Order $order) {
    return $user->id === $order->user_id;
});

Collection Higher Order Messages

Collection クラスにある集約やソートが、無名関数使わずに実行できるようになりました。

グループがDevelopmentの人たちの投票総数を返す例。

$users = User::where('group', 'Development')->get();

return $users->sum->votes;

Object Based Eloquent Events

Eloquent のsavedeletedのアクションがあった時のイベント通知をクラス指定出来るようになりました。

<?php

namespace App;

use App\Events\UserSaved;
use App\Events\UserDeleted;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The event map for the model.
     *
     * @var array
     */
    protected $events = [
        'saved' => UserSaved::class,
        'deleted' => UserDeleted::class,
    ];
}

Job Level Retry & Timeout

QueueJob には retrytimeout 設定がありましたが、それらはコマンドライン上では全てのJobに適応されていました。5.4からはJob単位で設定できます。

document: Queues

Request Sanitization Middleware

Middlewareに TrimStringsConvertEmptyStringsToNull が追加されました。

これらのmiddlwareは、自動的にrequestinputにある文字列の前後にある空白を削除し、空白の場合はnullにします。

これは全てのルーティングとコントローラーに、trim関数の実行結果をもたらします。

"Realtime" Facades

Facadesの指定がサービスコンテナーに書いていたのを、アプリケーションクラスに書くことで登録できるようになったで。という話っぽい。

PaymentGatewayというクラスをFacadesに登録したい場合はこんな感じ。

use Facades\ {
    App\Services\PaymentGateway
};

Route::get('/pay/{amount}', function ($amount) {
    PaymentGateway::pay($amount);
});

もちろんの事、Mockはこんな感じ。

PaymentGateway::shouldReceive('pay')->with('100');

Custom Pivot Table Models

中間テーブルを使う時に PivotModelインスタンスを作っていたのを、テーブル名指定で使うことができる。


namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    /**
     * The users that belong to the role.
     */
    public function users()
    {
        return $this->belongsToMany('App\User')->using('App\UserRole');
    }
}

Improved Redis Cluster Support

Redisのクラスターに対応したみたい。

Migration Default String Length

MySQL の utf8mb4 が migration で標準になったっぽい。🍣🍺問題は…

Laravel5.3からのアップグレードのときは、このcharacter set変更はしなくていいっぽい。

もし、character setutf8mb4に変えていて、 MySQLが5.7.7より古いときは、Migration 作成に手動でstring lengthを変えてやる必要がある。AppServiceProviderSchema::defaultStringLengthを呼び出せばいい。

use Illuminate\Support\Facades\Schema;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
}
34
33
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
33