$30 off During Our Annual Pro Sale. View Details »

Laravel でモデルの ID を UUID/ULID にする方法

SAW
December 08, 2023

Laravel でモデルの ID を UUID/ULID にする方法

Laravel勉強会@大阪 2023年12月 の発表資料です。

資料内リンク
- PHPカンファレンス関西2024 公式サイト
- https://2024.kphpug.jp/
- お誘いチケットについて
- https://note.com/phpcon_kansai/n/n4cec2c9bce88
- Laravel でテーブルの ID を ULID に指定する方法
- https://zenn.dev/azuki_penguin/articles/59715b43ef3b70
- Eloquent Model の ID を ULID に指定する方法
- https://zenn.dev/azuki_penguin/articles/6e78052e75180e

SAW

December 08, 2023
Tweet

More Decks by SAW

Other Decks in Programming

Transcript

  1. ׬

  2. NJHSBUJPOͷ࣮૷ྫ 12 Schema::create('users', function (Blueprint $table) { $table->uuid('id')->primary(); $table->string('name'); $table->string('email')->unique();

    $table->string('password'); }); Schema::create('users', function (Blueprint $table) { $table->ulid('id')->primary(); $table->string('name'); $table->string('email')->unique(); $table->string('password'); });
  3. GBDUPSZͷ࣮૷ྫ 14 public function definition(): array { return [ 'id'

    => Str::uuid(), 'name' => fake()->name(), 'email' => fake()->unique()->safeEmail(), 'password' => static::$password ??= Hash::make('password'), ]; } public function definition(): array { return [ 'id' => Str::ulid(), 'name' => fake()->name(), 'email' => fake()->unique()->safeEmail(), 'password' => static::$password ??= Hash::make('password'), ]; }
  4. &MPRVFOU.PEFMͷ*% &MPRVFOU.PEFMͷidϓϩύςΟ͸σϑΥϧτͰ੔਺ܕ ςʔϒϧଆ͕จࣈྻͰ΋&MPRVFOU.PEFMͰ͸੔਺஋ʹΩϟετ͞ΕΔ 16 > User::first() = App\Models\User {#7008 id:

    "01HGNMB0B0C35Q6WZ8BJ61V574", name: "Hector Nikolaus", email: "[email protected]", #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", created_at: "2023-12-02 16:03:44", updated_at: "2023-12-02 16:03:44", } > User::first()->id = 1 "01HGNMB0B0C35Q6WZ8BJ61V574"Λ੔਺ʹΩϟετͨ͠஋
  5. &MPRVFOU.PEFMͷ࣮૷ྫ 18 use Illuminate\Database\Eloquent\Concerns\HasUlids; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class User

    extends Model { use HasFactory, HasUlids; } use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class User extends Model { use HasFactory, HasUuids; }