0
1

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 3 years have passed since last update.

環境

・Mac OS catalina
・Laravel6系
・phpMyAdmin
・MAMP
・Visual Studio Code

シーディングとは

初期状態で、いくつかのダミーコードを作成すること。

手順

①シーダーファイルの作成

php artisan make:seeder (大文字)名前TableSeeder

②記述(シーディング処理)

public function run()
の中にダミーデータを記述していく。

public function run(){

//1レコード目
    $param = [
         'name' => 'taro', //キーがカラムにあたる
         'mail => 'taro@yamada.jp',
         'age' => 12,
    ];
    DB::table('(小文字)テーブル名')->insert($param);

//2レコード目
    $param = [
         'name' => 'hanako', //キーがカラムにあたる
         'mail => 'hanako@flower.jp',
         'age' => 34,
    ];
    DB::table('(小文字)テーブル名')->insert($param);
}

③シーダーファイルの登録

DatabaseSeeder.phpのファイルに、今から行うシーダーファイルを登録しておく必要がある。
public function run()に以下を記載する。

$this->call((大文字)名前TableSeeder::class);

$this->call(シーダークラス::class);という形で記載する。

④シーディングを実行する

php artisan db:seed

とターミナルで入力すると実行される。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?