Skip to content

Latest commit

 

History

History
348 lines (250 loc) · 9.19 KB

slides.md

File metadata and controls

348 lines (250 loc) · 9.19 KB
theme background class highlighter lineNumbers info drawings layout logoHeader website handle introImage
unicorn
text-center
shiki
false
## Slidev Starter Template Presentation slides for developers. Learn more at [Sli.dev](https://sli.dev)
persist
intro
./images/techtrain.jpg
suguru_ohki

Laravel9へ!

2021/02Laravel9がリリースされました!振り返りをしてみましょう。


Laravel9での把握すべきこと


概要

  1. PHP 8.0 が最低限必要
  2. Symfony6系を利用している
  3. 次のLTSとなる なりませんでしたw
version release date
Laravel 9.0 2022年2月(February 2022)
Laravel 10.0 2023年2月(February 2023)
Laravel 11.0 2024年2月(February 2024)

機能

Anonymous Migration Migration自体にそれぞれ名前をつける必要ない
New Query Builder Interface 「Query\Builder」「Eloquent\Builder」「Eloquent\Relation」の勘違いを減らす
PHP 8 String Functions \Illuminate\Support\Strクラスの内部でのstr_contains() 、 str_starts_with() 、およびstr_ends_with()のphpの標準関数の利用が始まる
From SwiftMailer to Symfony Mailer SymfonyがSwiftMailerを非推奨になったため、その解消

Anonymous Migration


そもそもMigration自体にそれぞれ名前をつける必要があるんだっけ?という疑問から生まれた機能。

該当Issueはこちらで参考の書き方は次のとおり。

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('people', function (Blueprint $table)
        {
            $table->string('first_name')->nullable();
        });
    }
};

Before

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddColumnFirstNameInPeopleTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('people', function (Blueprint $table) {
            $table->string('first_name')->nullable();
        });
    }
};

After

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('people', function (Blueprint $table)
        {
            $table->string('first_name')->nullable();
        });
    }
};

New Query Builder Interface


クエリビルダインターフェースが提供されるようになる。これにより補完周りがかなり便利に。 こちら のプルリクエストにもあるように、補完が効いているときに、「Query\Builder」なのか「Eloquent\Builder」なのか「Eloquent\Relation」なのかによって補完が間違っているせいで一度エラーが出たなんて経験がある程度補完を使ったことがある人なら誰しもあるはず。そのエラーが出なくなるらしい。

<?php

return Model::query()
	->whereNotExists(function($query) {
		// $query is a Query\Builder
	})
	->whereHas('relation', function($query) {
		// $query is an Eloquent\Builder
	})
	->with('relation', function($query) {
		// $query is an Eloquent\Relation
	});

PHP 8 String Functions


\Illuminate\Support\Strクラスの内部での

  • str_contains()
  • str_starts_with()
  • str_ends_with()

のphpの標準関数の利用が始まる。今までphpの標準関数に文字列の中に特定nの文字が含まれるのかなどといった関数がなかったため、Laravelでは、独自のラッパーメソッドを実装していた。今回でその独自実装でなくなり、標準関数の利用が始まる

プルリクエストは こちら

Since PHP 8 will be the minimum, Tom Schlick submitted a PR to move to using str_contains(), str_starts_with() and str_ends_with() functions internally in the \Illuminate\Support\Str class.

From SwiftMailer to Symfony Mailer


プルリクエストはこちら

SymfonyがSwiftMailerを非推奨としたため、Laravel9で、すべてのメールトランスポートにSymfonyMailerを使用するように変更された

Symfony deprecated SwiftMailer and Laravel 9 makes the change to use Symfony Mailer for all the mail transports. This does open up a few breaking changes and you can review the PR for all the details. The Laravel 9 upgrade guide will include instructions once it's officially released.

Test Coverage


php artisan test --coverage

アップデートの鬼門


LTSにおけるアップデート(6.x -> 9.x)について

  1. Laravel8での変更が鬼門となりそう
  2. モデルファクトリが8からクラスベースの書き方に変更があった。
  3. しばらくは一旦ヘルパーで捌けるとはいえ、長期的なことを考えると、書き換えておいた方が良さそう

Before

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Models\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'email_verified_at' => now(),
        'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        'remember_token' => Str::random(10),
    ];
});

After

use Illuminate\Database\Eloquent\Factories\Factory;

class UserFactory extends Factory
{
    protected $model = User::class;
    public function definition()
    {
        return [
            'name' => $this->faker->name,
            'email' => $this->faker->unique()->safeEmail,
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    }
}
  1. HasFactoryのtraitをModelに入れる必要がある
  2. ファクトリにネームスペースが付くため、ネームスペースをcomposer.jsonに追加

composer.jsonの修正

"fideloper/proxy"

こちらは削除します。


protected $headers = Request::HEADER_X_FORWARDED_ALL;
// ↓のようにアップデートする
protected $headers =
        Request::HEADER_X_FORWARDED_FOR |
        Request::HEADER_X_FORWARDED_HOST |
        Request::HEADER_X_FORWARDED_PORT |
        Request::HEADER_X_FORWARDED_PROTO |
        Request::HEADER_X_FORWARDED_AWS_ELB;

周辺ライブラリのアップデートについて(人権的なライブラリたち)


  1. phpstan(larastan)
  2. phpunit
  3. php-cs-fixer
  4. paratest
  5. laravel-ide-helper
  6. composer

Rectorを使って乗り越えられるかもしれない。


Rector とは

https://github.com/rectorphp/rector


カオナビさんの素晴らしいアドベントカレンダー


layout: center class: text-center


Learn More

Documentations · GitHub · Showcases