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

【Laravel】マイグレーションでテーブルを作成する

Posted at

こちらの記事は以下の書籍を参考にアウトプットとして執筆しました。
PHPフレームワーク Laravel入門 第2版

マイグレーションでテーブルを作成する手順は以下

  1. 専用スクリプト作成
  2. スクリプトへ記述
  3. マイグレーションの実行

今回は以下のテーブルを作成していく

テーブル名はmembers

カタム フィールド名
ID id
作成日時 created_at
ユーザー名 name
パスワード password
メールアドレス email
プロフィール写真 icon

専用スクリプト作成

カレントディレクトリへ移動して以下のコマンドを実行

$ php artisan make:migration ファイル名

ファイル名はどのような作業をするのかをわかるようにしておく。
例えば
今回のような場合は作成するのであれば

$ php artisan make:migration create_members_table

としてファイルを作成する。

databaseフォルダをチェック

作成したファイルはdatabase/migrationsの中に作成される。

マイグレーション処理

作成されたphpファイルいついて

upメソッド

テーブル生成のための処理を記述

デフォルトではidカラムとライムスタンプの項目を追加するコードが書かれている。

downメソッド

テーブル削除の処理を書く

デフォルトではテーブルが有るなら消すという処理が書かれている。

この2つのメソッドはマイグレーションを実行したときに呼ばれる。

テーブル生成の処理

Schema::create

テーブル生成はSchema::createを使う。

構文
Schema::create(テーブル名,function(Blueprint %table){
//テーブル生成処理
});

参考:PHPフレームワーク Laravel入門 第2版

マイグレーションの実行でテーブル自体は作られ、フィールドはこのクロージャーに書く。

というわけで以下のように書き換える

public function up()
    {
        Schema::create('members', function (Blueprint $table) {
            $table->increment('id');
            $table->dateTime('created_at');
            $table->string('name');
            $table->integer('password');
            $table->string('email');
            $table->string('icon');
        });
    }

マイグレーションを実行

$ phpa artisan migrate

しかしエラー


   Illuminate\Database\QueryException

  SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667|         // If an exception occurs when attempting to run a query, we'll format the error
    668|         // message to include the bindings with SQL, which will make this exception a
    669|         // lot more helpful to the developer instead of just the database's errors.
    670|         catch (Exception $e) {
  > 671|             throw new QueryException(
    672|                 $query, $this->prepareBindings($bindings), $e
    673|             );
    674|         }
    675|

      +37 vendor frames
  38  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

これはdatabase.php.envのDB_HOSTをlocalhostにすることで解決した。

.env
DB_HOST=localhost
database.php
'host' => env('DB_HOST', 'localhost'),
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?