0
0

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 1 year has passed since last update.

[Laravel] テーブル作成手順

Posted at

データベース作成

主人公は4人:

  • マイグレーションファイル: データベースの作成。どんなカラムがあるかなどの設定も必要。
  • モデルファイル: データベースの形状の設定。どんなカラムがあるかなど。
  • ファクトリーファイル: ランダムデータ生成項目の設定。何文字かなど。
  • シーダーファイル: ランダムデータ生成アクションの設定。いくつのデータを生成するかなど。

テーブル作成・内容設定編集・適用

作成: $ sail artisan make:migration create_tag_table

編集:

database/migrations/{date}_create_tag_table.php
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->id();
+           $table->string('tag_name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('tags');
    }
};

適用:

$ sail artisan migrate
   INFO  Running migrations.  
   2023_12_04_171640_create_tag_table ............................... 66ms DONE

適用の巻き戻し:
$ sail artisan migrate:rollback

必要ファイル作成

作成:
$ sail artisan make:model {model name} --all

  • オプション: 各ファイルを同時に生成する。
    --migration--factory--seed--controller--all
$ sail artisan make:model tag --migration --controller
   INFO  Model [app/Models/tag.php] created successfully.  
   INFO  Migration [database/migrations/2023_12_04_164729_create_tags_table.php] created successfully.  
   INFO  Controller [app/Http/Controllers/TagController.php] created successfully.  

モデル設定

  • Eloquent ORM (Object Relational) Mapper: Laravel で用いられている。コードとデータベースを対にさせる設定。

編集:

app/Models/Tag.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class tag extends Model
{
    use HasFactory;
+   protected $table = 'tags';
+   protected $primarykey = 'tag_id';  // needed if primary key name != 'id'
}

ファクトリー編集

  • Laravel 内のランダムデータの生成設定

編集:

database/factory/TagFactory.php
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\ag>
 */
class TagFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
+           'tag_name' => $this->faker->realText(100)
        ];
    }
}
  • config/app.php の 'faker_locale' を 'ja_JP' にすると日本語のランダムデータが生成される設定になる

シーダー設定

  • ファクトリーの適用設定

database/seeders/TagSeeder.php
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
+ use App\Models\Tag;

class tag extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
+       Tag::factory()->count(5)->create();
    }
}

ファクトリー・シーダー適用

$ sail artisan db:seed --class=TagSeeder
   INFO  Seeding database.  

データベースが更新される
image.png

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?