6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

お試しLaravel5.4

Last updated at Posted at 2017-01-31

laravel 5.4がリリースされたみたいなので気になったところだけ試してみようと思います。

Sanitizing Middleware

5.4で新たにTrim Strings MiddlewareとConvert Empty Strings to NullというMiddlewareが追加されました。
前後の空白除いたり空文字をnullにしてくれます。

  • TrimString
Illuminate\Foundation\Http\Middleware\TrimString.php
protected function transform($key, $value)
{
    if (in_array($key, $this->except)) {
        return $value;
    }

    return is_string($value) ? trim($value) : $value;
}
  • ConvertEmptyStringsToNull
Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull.php
protected function transform($key, $value)
{
    return is_string($value) && $value === '' ? null : $value;
}

Realtime Facades

今までだとサービスコンテナにclassを登録してFacadeを利用していましたが、5.4からclassを直接Facadeとして扱うことができるようになったみたいです。

例)

App\Greeting.php
<?php

namespace App;

class Greeting
{
    public function sayHello() {
        return 'hello';
    }
}

routeやcontrollerから以下のように呼び出せます。

routes\wep.php
<?php

use Facades\ {
    App\Greeting
};

Route::get('/greeting', function() {
    return Greeting::sayHello();
});

名前空間は
use Facades\App\Greeting;
とかでもいけます。

Higher Order Messaging for Collections

collectionに対するメソッドが簡潔に記述できるようになりました。

例)

Taskテーブルに以下のようなデータが入っているとします。
スクリーンショット 2017-02-01 6.42.02.png

モデルにメソッドを記述

App\Task.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
  protected $table = 'Task';
  protected $fillable = ['complete_flag'];

  public function complete() {
    $this->update(['complete_flag' => 1]);
  }

}

Controllerで実行

App\Http\Controllers\TaskController.php
<?php

namespace App\Http\Controllers;

use App\Task;

class TaskController extends Controller
{
    public function index() {
      $tasks = Task::all();

      /* 5.3以下の記述方法
      $tasks->each(function($task) {
        $task->complete();
      });
      */
      
      $tasks->each->complete();

      return;
    }
}
routes\web.php
<?php

Route::get('/task', 'TaskController@index');

これで/taskにアクセスしTaskテーブルを確認するとcompleteメソッドが実行されたのが分かるかと思います。
スクリーンショット 2017-02-01 6.48.33.png

この処理の記述はIlluminate\Support\HigherOrderCollectionProxyにされています。

Illuminate\Support\HigherOrderCollectionProxy.php
<?php

namespace Illuminate\Support;

class HigherOrderCollectionProxy
{
    /**
     * The collection being operated on.
     *
     * @var \Illuminate\Support\Collection
     */
    protected $collection;

    /**
     * The method being proxied.
     *
     * @var string
     */
    protected $method;

    /**
     * Create a new proxy instance.
     *
     * @param  \Illuminate\Support\Collection  $collection
     * @param  string  $method
     * @return void
     */
    public function __construct(Collection $collection, $method)
    {
        $this->method = $method;
        $this->collection = $collection;
    }

    /**
     * Proxy accessing an attribute onto the collection items.
     *
     * @param  string  $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->collection->{$this->method}(function ($value) use ($key) {
            return is_array($value) ? $value[$key] : $value->{$key};
        });
    }

    /**
     * Proxy a method call onto the collection items.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->collection->{$this->method}(function ($value) use ($method, $parameters) {
            return $value->{$method}(...$parameters);
        });
    }
}

他にもBladeの記法やMailがマークダウンで記述可能になったりと変更があったみたいです。

↓まとめてくださってます。
Laravel 5.4 変更点のメモ

また新しくブラウザテストツールのLaravel Duskなども導入されたみたいなので後で触ってみようと思います。

【参考】
https://laravel-news.com/laravel-5-4
https://laravel.com/docs/5.4/releases

6
3
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
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?