0
0

More than 3 years have passed since last update.

データベースを使用して処理を書いてみる備忘録(同じ事前も書いたけど)

Posted at

データベースの処理を前の記事でも書いたが再度復習として備忘録します。まとめます。

マイグレーションファイルを作成する。

今回は投稿記事としてarticleテーブルを作成します。

ターミナル
php artisan make:migration create_articles_table --create=articles

パスは

database/migrations/2020_12_23_191814_create_article_table
で作られています。

中身は少し書き換えたんでデフォルトより書き換わってますが、

2020_12_23_191814_create_article_table
<?php

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

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->string('article_title');追加
            $table->string('article');追加
            $table->timestamps();
        });
    }

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

マイグレーションを実行する

作成したマイグレーションファイルを元にマイグレーションを実行する。

ターミナル
php artisan migrate

因みに自分のsqliteのバージョン

ターミナル
sqlite3 --version
3.34.0 2020-12-01 16:14:00 a26b6597e3ae272231b96f9982c3bcc17ddec2f2b6eb4df06a224b91089fed5b

数字の羅列があるが大丈夫なのか??
とりあえずプロジェクトディレクトリまで移動する。

データベースに追加してみる。

ターミナル
sqlite3 database/database.sqlite 

実行結果

ターミナル
SQLite version 3.34.0 2020-12-01 16:14:00

テーブルを確認する

データベースへ接続ができたら、以下コマンドで現存するテーブルを確認します。

ターミナル
sqlite> .table

実行結果、以前作成したテーブルもあるので合計六つのテーブルが確認できます。

ターミナル
articles         migrations       users
failed_jobs      password_resets  votings
  • articlesテーブル
  • migrationsテーブル
  • usersテーブル
  • failed_jobsテーブル
  • password_resetsテーブル
  • votingsテーブル

以上がテーブル一覧です。

テーブル定義を確認する

ターミナル
.schema articles

実行結果

ターミナル

CREATE TABLE IF NOT EXISTS "articles" (
      "id" integer not null primary key autoincrement, 
      "article_title" varchar not null, 
      "article" varchar not null, 
      "created_at" datetime null, 
      "updated_at" datetime null
);

とりあえずこれで、、
間違いはあると思うのでなにかあればご指摘頂ければ幸いです。

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