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

まずデータベースを作成するにはmigrationファイルが必要なので、
artisanコマンドを利用してデータベースを生成するmigrationファイルを生成します。

migrationファイルとは
マイグレーションとはデータベースのバージョンコントロールのような機能です。

php artisan make:migration create_voting_table --create=voting
Created Migration: 2020_12_12_192229_create_voting_table

となると成功です。
これをやると、
プロジェクトフォルダ/database/migration/の下にファイルが生成されます。
今回はvoting_tableを生成したのでそれがmigrationファルダ内に作られています。
migrationフォルダ内には他にデフォルトでユーザーの管理用テーブルとパスワードテーブルが生成されています。
デフォルトというか、プロジェクトを生成した時点で作られます。

因みにファイルの中身は以下になります

2020_12_12_192229_create_voting_table.php
<?php

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

class CreateVotingTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('voting', function (Blueprint $table) {
            $table->id();
            $table->string('name');べた書きで加えました
            $table->timestamps();
        });
    }

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

自身でテーブル内を書き加えました。
書き加えたら保存します。
そうしたらartisanコマンドでマイグレーションを実行します。

コマンドプロンプト
C:\neomediaproject>php artisan migrate

Illuminate\Database\QueryException

  Database (\neomediaproject\database\database.sqlite) does not exist. (SQL: PRAGMA foreign_keys = ON;)

  at C:\neomediaproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php:678
    674▕         // If an exception occurs when attempting to run a query, we'll format the error
    675▕         // message to include the bindings with SQL, which will make this exception a
    676▕         // lot more helpful to the developer instead of just the database's errors.
    677▕         catch (Exception $e) {
  ➜ 678▕             throw new QueryException(
    679▕                 $query, $this->prepareBindings($bindings), $e
    680▕             );
    681▕         }
    682▕

  1   C:\neomediaproject\vendor\laravel\framework\src\Illuminate\Database\Connectors\SQLiteConnector.php:34
      InvalidArgumentException::("Database (\neomediaproject\database\database.sqlite) does not exist.")

  2   C:\neomediaproject\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php:222
      Illuminate\Database\Connectors\SQLiteConnector::connect()

ん?うまくいかないぞ、、

コマンドプロンプト
 Database (\neomediaproject\database\database.sqlite) does not exist. (SQL: PRAGMA foreign_keys = ON;)

ここを見る限りファイルがないてきな感じかな

なので

コマンドプロンプト
>sqlite3 database.sqlite

ファイル作りました!!

んでもう一度artisanしてみます。

コマンドプロンプト
php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (42.77ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (23.43ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (23.97ms)
Migrating: 2020_12_12_192229_create_voting_table
Migrated:  2020_12_12_192229_create_voting_table (9.82ms)

うまくいきました。

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?