LoginSignup
0
0

More than 3 years have passed since last update.

PHP Laravel 6 おすすめ映画投稿サイト作成過程 8:ジャンルタグ追加編(前編)

Posted at

ジャンルタグの追加

映画のジャンルをタグで分けられるようにします

必要なファイルの作成

※-a はallオプション

$ php artisan make:model Modesl/Tag -a

マイグレーションファイルの設定

タグは以下の5種類にしますが、いずれもタイトルカラムの一つとなります。
よって、追加するのはタイトルカラムのみです。

タグ:SF アクション コメディ ドキュメンタリー サスペンス

recommend/database/migrations/2020_09_25_124109_create_tags_table.php
 public function up()
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->timestamps();
        });
    }

初期データの作成

まず、シーダーファイルを作成します。
Screen Shot 2020-09-25 at 12.57.07.png

次に、以下の要領でタグを作成します。

recommend/database/seeds/RecommendTagSeeder.php
<?php

use Illuminate\Database\Seeder;
use App\Models\Tag;

class RecommendTagSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Tag::create([
          'title' => 'SF',
          'title' => 'アクション',
          'title' => 'コメディ',
          'title' => 'ドキュメンタリー',
          'title' => 'サスペンス',
        ]);
      }
    }

データベースの作成

$ php artisan migrate

シーダの実行

オートローダを再生成します。

$ composer dump-autoload

シーダーを読み込みます。
今回は読み込むシーダーを指定するため、以下のようにします。
なお、指定しない場合はDatabaseSeederクラスが実行されます。

$ php artisan db:seed --class=RecommendTagSeeder
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