各ページより
非公式ドキュメント(日本語訳)の各ページを5.4との差分を見ながら気になるものだけ追加していきます。
サービスコンテナ
サービスコンテナのPSR-11コンテナインターフェース呼び出し
use Psr\Container\ContainerInterface;
Route::get('/', function (ContainerInterface $container) {
$service = $container->get('Service');
//
});
HTTPリクエスト
intersectメソッドの削除
onlyメソッドのロジック修正にて後述
onlyメソッドのロジック修正
5.4ではonly()
の引数に存在しないキーを入れるとそのキーの値にはnull
が入り返却されていたが、
5.5からはonly()
は存在するキーの値のみ返却されるようになった。
(5.4までのintersect()
と同様の機能のためintersect()
は消えた)
/**
* Get a subset containing the provided keys with values from the input data.
*
* @param array|mixed $keys
* @return array
*/
public function only($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
$results = [];
$input = $this->all();
foreach ($keys as $key) {
Arr::set($results, $key, data_get($input, $key));
}
return $results;
}
/**
* Get a subset containing the provided keys with values from the input data.
*
* @param array|mixed $keys
* @return array
*/
public function only($keys)
{
$results = [];
$input = $this->all();
$placeholder = new stdClass;
foreach (is_array($keys) ? $keys : func_get_args() as $key) {
$value = data_get($input, $key, $placeholder);
if ($value !== $placeholder) {
Arr::set($results, $key, $value);
}
}
return $results;
}
hasメソッドのロジック修正
5.4ではisEmptyString()
を通して値を取得していたが、
5.5では存在する値かどうかのみを見るよう修正されている
これは後述のfilledメソッドの追加により、機能を分割したためと思われる
/**
* Determine if the request contains a non-empty value for an input item.
*
* @param string|array $key
* @return bool
*/
public function has($key)
{
$keys = is_array($key) ? $key : func_get_args();
foreach ($keys as $value) {
if ($this->isEmptyString($value)) {
return false;
}
}
return true;
}
/**
* Determine if the request contains a given input item key.
*
* @param string|array $key
* @return bool
*/
public function has($key)
{
$keys = is_array($key) ? $key : func_get_args();
$input = $this->all();
foreach ($keys as $value) {
if (! Arr::has($input, $value)) {
return false;
}
}
return true;
}
filledメソッドの追加
リクエストに値が存在しており、かつ空でないかを確認する
if ($request->filled('name')) { // }
バリデーション
formatValidationErrors & formatErrorsメソッドの削除
前述のConsistent Exception Handlingにより削除(移動)
エラーとログ
The report Helper
「例外を報告はするが、現在のリクエストを処理し続ける」という際に使用するヘルパー関数の実装
public function isValid($value)
{
try {
// Validate the value...
} catch (Exception $e) {
report($e);
return false;
}
}
Bladeテンプレート
sectionディレクティブのTips
Tip!! Contrary to the previous example, this sidebar section ends with @endsection instead of @show. The @endsection directive will only define a section while @show will define and immediately yield the section.
@endsection
はセクションを定義するだけだが、@show
は定義して直ちに出力する
意味がよくわからないが、親テンプレートは@show
を使い、子テンプレートは@endsection
を使うってことかな?
Laravel Recipes
この話がTipsとして表示されたということだろうか。
Switch Statements
bladeにswitch文の追加
@switch($i)
@case(1)
First case...
@break
@case(2)
Second case...
@break
@default
Default case...
@endswitch
eachディレクティブのNote
Views rendered via @each do not inherit the variables from the parent view. If the child view requires these variables, you should use @foreach and @include instead.
@each
を介してレンダリングされたビューは、親ビューから変数を継承しません。 子ビューにこれらの変数が必要な場合は、代わりに@foreach
と@include
を使用する必要があります。
ヘルパ
tapメソッドの追加
The tap function accepts two arguments: an arbitrary \$value and a Closure. The $value will be passed to the Closure and then be returned by the tap function. The return value of the Closure is irrelevant:
tap関数は、任意の$value
とClosure
の2つの引数を受け取ります。
$value
はクロージャに渡され、 tap関数によって返されます。 Closure
の戻り値は無関係です。
$user = tap(User::first(), function ($user) {
$user->name = 'taylor';
$user->save();
});
If no Closure is passed to the tap function, you may call any method on the given $value. The return value of the method you call will always be \$value, regardless of the what the method actually returns in its definition. For example, the Eloquent update method typically returns an integer. However, we can force the method to return the model itself by chaining the update method call through the tap function:
Closure
がtap関数に渡されない場合、指定された$value
に対して任意のメソッドを呼び出すことができる。
呼び出すメソッドの戻り値は、メソッドが実際にその定義に返すものに関係なく、常に$value
になります。
たとえば、Eloquentのupdate
メソッドは通常、整数を返します。 しかし、update
メソッド呼び出しをtap関数を通して連鎖させることによって、メソッドがモデル自体を返すように強制することができます。
$user = tap($user)->update([
'name' => $name,
'email' => $email
]);
リレーション
loadMissingメソッドの追加
まだloadしていないときのみloadする場合に使用する
public function format(Book $book)
{
$book->loadMissing('author');
return [
'name' => $book->name,
'author' => $book->author->name
];
}