Laravel 5.3でdatabase & migrationを設定する


## データベース接続情報の設定

* config/database.phpの内容を確認。
	
	
	``` config/database.php
	...
	
	    /*
	    |--------------------------------------------------------------------------
	    | Default Database Connection Name
	    |--------------------------------------------------------------------------
	    |
	    | Here you may specify which of the database connections below you wish
	    | to use as your default connection for all database work. Of course
	    | you may use many connections at once using the Database library.
	    |
	    */
	
	    'default' => env('DB_CONNECTION', 'mysql'),
	
	    /*
	    |--------------------------------------------------------------------------
	    | Database Connections
	    |--------------------------------------------------------------------------
	    |
	    | Here are each of the database connections setup for your application.
	    | Of course, examples of configuring each database platform that is
	    | supported by Laravel is shown below to make development simple.
	    |
	    |
	    | All database work in Laravel is done through the PHP PDO facilities
	    | so make sure you have the driver for your particular database of
	    | choice installed on your machine before you begin development.
	    |
	    */
	
	    'connections' => [
	
	        'sqlite' => [
	            'driver' => 'sqlite',
	            'database' => env('DB_DATABASE', database_path('database.sqlite')),
	            'prefix' => '',
	        ],
	
	        'mysql' => [
	            'driver' => 'mysql',
	            'host' => env('DB_HOST', 'localhost'),
	            'port' => env('DB_PORT', '3306'),
	            'database' => env('DB_DATABASE', 'forge'),
	            'username' => env('DB_USERNAME', 'forge'),
	            'password' => env('DB_PASSWORD', ''),
	            'charset' => 'utf8',
	            'collation' => 'utf8_unicode_ci',
	            'prefix' => '',
	            'strict' => true,
	            'engine' => null,
	        ],
	
	        'pgsql' => [
	            'driver' => 'pgsql',
	            'host' => env('DB_HOST', 'localhost'),
	            'port' => env('DB_PORT', '5432'),
	            'database' => env('DB_DATABASE', 'forge'),
	            'username' => env('DB_USERNAME', 'forge'),
	            'password' => env('DB_PASSWORD', ''),
	            'charset' => 'utf8',
	            'prefix' => '',
	            'schema' => 'public',
	            'sslmode' => 'prefer',
	        ],
	
	    ],
	...
	```

* 上記のような内容なので、設定値は.env.xxxxxで変更する。
	
	``` .env.xxxxx
	...
	DB_CONNECTION=mysql
	DB_HOST=(your-database-host)
	DB_DATABASE=(your-database-database)
	DB_USERNAME=(your-database-username)
	DB_PASSWORD=(your-database-password)
	...
	```

## migrationの設定

* config/database.phpの内容を確認。
* 'migrations'は、migration情報を管理するためのテーブル名。テーブル名を変更したい場合はここを変更する。

	``` config/database.php
	...
	
	    /*
	    |--------------------------------------------------------------------------
	    | Migration Repository Table
	    |--------------------------------------------------------------------------
	    |
	    | This table keeps track of all the migrations that have already run for
	    | your application. Using this information, we can determine which of
	    | the migrations on disk haven't actually been run in the database.
	    |
	    */
	
	    'migrations' => 'migrations',
	...
	```
	


* migration定義の作成。

	``` console
	cd (your/laravel/project/path)
	php artisan make:migration create_your_table_name
	
	(結果出力)
	Created Migration: 2016_09_23_115113_create_your_table_name
	```

* 上記を実行すると、database/migrations配下に、yyyy_mm_dd_hhmmss_create_your_table_name.phpが作成される。

	``` yyyy_mm_dd_hhmmss_create_your_table_name.php
	<?php
	
	use Illuminate\Support\Facades\Schema;
	use Illuminate\Database\Schema\Blueprint;
	use Illuminate\Database\Migrations\Migration;
	
	class CreateYourTableName extends Migration
	{
	    /**
	     * Run the migrations.
	     *
	     * @return void
	     */
	    public function up()
	    {
	        //
	    }
	
	    /**
	     * Reverse the migrations.
	     *
	     * @return void
	     */
	    public function down()
	    {
	        //
	    }
	}
	```

	* up/downメソッドの定義を書いていく。

## migrationの実行

* migrationを実行してテーブルを作成する

	``` console
	cd (your/laravel/project/path)
	APP_ENV=(your-laravel-environment) php artisan migrate
	```

* すべてのテーブルを再作成するには

	``` console
	cd (your/laravel/project/path)
	APP_ENV=(your-laravel-environment) php artisan migrate:refresh