
[Laravel] 6.5.0がリリースされました
laravel/frameworkのバージョン6.5.0がリリースされました。追加された機能について確認します。
LazyCollectionクラスにremember()メソッドが追加されました (#30443)
remember()メソッドを呼び出すと、実行済みのクエリの結果がキャッシュに保存される新しいLazyCollectionが返されます。
再実行された時にすでに実行済みのインデックスはキャッシュが参照されます。
$users = User::cursor()->remember();
// No query has been executed yet.
$users->all();
// All values have been pulled from the DB.
$users->all();
// We did not hit the DB again. We got the users from `remember`'s cache.Str::afterLast()とStr::beforeLast()メソッドが追加されました (#30507)
すでにHelperメソッドとして提供されていたStr::after()やStr::before()と似ていますが、返却される値が検索ワードが最後にヒットした前後の値です。
$type = 'App\Notifications\Tasks\TaskUpdated';
Str::afterLast($type, '\\'); // TaskUpdated
Str::after($type, '\\'); // Notifications\Tasks\TaskUpdated
$filename = 'photo.2019.11.04.jpg';
Str::beforeLast($filename, '.'); // photo.2019.11.04
Str::before($filename, '.'); // photoクエリビルダークラスにexistsOr()とdoesntExistOr()メソッドが追加されました (#30495)
2つのメソッドは引数としてClosureを受け取り、条件に一致しなかった場合にはコールバックが実行委されます。
// before
$hasOpenDossier = $user->dossiers()
    ->whereNull('closed_at')
    ->exists();
if ($hasOpenDossier) {
    abort(422, 'User already has an open dossier');
}
// after
$user->dossiers()
    ->whereNull('closed_at')
    ->doesntExistOr(function () {
        abort(422, 'User already has an open dossier');
    });Bladeでカスタムしたif文を作成した場合にunlessディレクティブが自動で作成されるようになりました (#30492)
Blade::if()メソッドは現在、次の3つのディレクティブを自動的に登録していました
- @env (positive if condition)
- @elseenv (elseif condition)
- @endenv (endif)
@env('local')
    // The application is in the local environment...
@elseenv('testing')
    // The application is in the testing environment...
@else
    // The application is not in the local or testing environment...
@endenvこの変更により上記の3つに加えてunlessディレクティブが自動的に登録されるようになりました。
@unlessenv('production')
    // The application is not in the production environment...
@endenv詳しい変更については以下を確認してください。