現金出納簿2 テーブルtypes

テーブル設計。ER図作成中。

マイグレーションを作る。

php artisan make:migration create_types_table

マイグレーションでテーブルを定義する。

Schema::create('types', function (Blueprint $table) {
 $table->id();
 $table->string('type_name', 100);
 $table->timestamp('updated_at')->useCurrent()->nullable();
 $table->timestamp('created_at')->useCurrent()->nullable();
});

マイグレーションを実行。

php artisan migrate

シーダーを使ってデーターを入力する。

php artisan make:seeder TypeSeeder
use Illuminate\Support\Facades\DB; //追記

  public function run()
  {
    DB::table('types')->insert(
      [
        [
          'type_name' => '収入',
          'created_at' => now(),
          'updated_at' => now(),
        ],
        [
          'type_name' => '支出',
          'created_at' => now(),
          'updated_at' => now(),
        ],
     ]
    );
  }

database/seeders/DatabaseSeeder.php を編集。

public function run()
    {
        $this->call([
        TypeSeeder::class
        ]);

    }

シーターを実行。これでデータが格納された。

php artisan db:seed --class=TypeSeeder

モデルを作成。

php artisan make:model Type
public function categories()
{
   return $this->hasMany('App\Models\Category');
}

コメント