LoginSignup
4
4

More than 5 years have passed since last update.

Laravelインストール直後の設定変更、テーブル作成

Posted at

Laravel勉強し始めたのでメモです。
- 参考サイト:Laravel 5.7 インストール

タイムゾーン、ローケールの設定

対象ファイル:[app\config\app.php]

デフォルトではUTCとenなので、日本で使う場合は以下のようにします。

|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => 'Asia/Tokyo',

/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by the translation service provider. You are free to set this value
| to any of the locales which will be supported by the application.
|
*/

'locale' => 'ja',

データベースの設定

対象ファイル:[app\config\database.php]

mysqlを使用しますので、以下を環境に合わせて変更します。

  • DB_DATABASE
  • DB_USERNAME
  • DB_PASSWORD

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
    

マイグレーション

対象ファイル:[app.env]

マイグレーションする場合は設定します。

DB_CONNECTION=mysql
DB_HOST=DBサーバーのアドレス
DB_PORT=3306
DB_DATABASE=作成したDB
DB_USERNAME=ユーザ名
DB_PASSWORD=パスワード

マイグレーション準備

対象ファイルが作成される場所:[app\database\migrations\xxx.php]

テーブル構造を定義するファイルを作成します。

artisanを使用して、マイグレーションファイルを作成します。
以下を作成します
- URL管理テーブル:t_url_list
- OSを管理するテーブル(マスタ)m_os_type

# php artisan make:migration create_url_list_table  --create=t_url_list
# php artisan make:migration create_os_type_master  --create=m_os_type

実行すると[app\database\migrations]の下に、タイムスタンプがプリフィックスとして
付加された、create_url_list_table.phpとcreate_os_type_master.phpが作成されます。
(例)2019_01_17_180850_create_url_list_table.phpなど
その二つのファイルにテーブルのカラムを追加します。
- 参考:Laravel 5.7 データベース:マイグレーション -> 使用できるカラムタイプ
- 参考:英語版:Laravel 5.7 データベース:マイグレーション Creating Columns

テーブル構造を定義します。

t_url_listテーブル

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUrlListTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('t_url_list', function (Blueprint $table) {
            $table->bigIncrements('id'); // Auto-incrementing UNSIGNED BIGINT (primary key) equivalent column.
            $table->string('url', 100); //VARCHAR equivalent column with a optional length.
            $table->unsignedInteger('m_os_type_id'); //UNSIGNED INTEGER equivalent column.
            $table->dateTime('created');
            $table->dateTime('modified');
            // インデックス追加
            $table->index('m_os_type_id', 'idx_t_url_list_001'); 
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('t_url_list');
    }
}

m_os_typeマスター

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOsTypeMaster extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('m_os_type', function (Blueprint $table) {
            $table->increments('id'); // Auto-incrementing UNSIGNED INTEGER (primary key) equivalent column.
            $table->string('os_type_name', 20)->unique(); // VARCHAR equivalent column with a optional length. ユニークにする
            $table->dateTime('created');
            $table->dateTime('modified');
       });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('m_os_type');
    }
}

artisanでマイグレーションを行います


$ php artisan migrate
Migrating: 2019_01_17_180850_create_url_list_table
Migrated:  2019_01_17_180850_create_url_list_table
Migrating: 2019_01_17_180858_create_os_type_master
Migrated:  2019_01_17_180858_create_os_type_master

上記のように表示されたらOKです。descコマンドや、phpMyAdminなどで構造を確認します。

m_os_type 定義通りテーブルが作成されています(もう一つは割愛)

スクリーンショット 2019-01-18 15.42.59.png

長くなったので、データの初期値登録は別にします。

4
4
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
4
4