LoginSignup
2
0

More than 3 years have passed since last update.

sqliteを使ってCRUDができるサイトを作ってみた(前編)

Last updated at Posted at 2019-04-03

はじめに

このブログは2部構成です。
前編では、プロジェクト、コントローラー、モデル、テーブルの作成、サイトへのアクセスを
後編では、CRUDの作り方、データの1連の流れを紹介していきます。

狙い

mysqlのバージョンをあげてからmysqlを使えなくなった。
(migrationコマンドは通るが、データの登録が通らないのでsqliteを使うことでデータの登録、更新ができた。)

バージョン(2019年4月3日時点)

$ php -v
PHP 7.3.3 (cli) (built: Mar  8 2019 16:40:07) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.3, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.3, Copyright (c) 1999-2018, by Zend Technologies

開発環境

vagrantを使う。

vagrantを使用した開発環境の構築についてはここでは紹介しない。

仮想環境にアクセスする

$vagrant up

でvagrantを立ち上げる。

$vagrant ssh

で、vagrantにssh接続する

vagrant@homestead:~$ cd /vagrant/

することでホームディレクトリに移動する

プロジェクトを作成する

vagrant@homestead:/vagrant$ composer create-project laravel/laravel example

プロジェクトに移動する

vagrant@homestead:/vagrant$ cd example/
vagrant@homestead:/vagrant/example$ 

コントローラーとモデルを作成する

上:コントローラー、下:モデル(migrationファイルも作成される)

php artisan make:controller ExampleController -r
php artisan make:model Example -m

.envを編集する

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

DB_CONNECTION=sqlite

にした後でsqliteを使うために以下コマンドをうちます。


$touch database/database.sqlite

ここまで来たらもう少しです。頑張ってください。

migrationファイルの作成

database/migration/20YY_MM_DD_HHMMSS_create_examples_table.php

ファイルを編集していく

public function up()
{
    Schema::create('examples', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->timestamps();
    });
}
public function up()
{
    Schema::create('examples', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('title');//追加
        $table->text('content');//追加
        $table->timestamps();
    });
}

ちなみに、create_user,create_passwordテーブルは今回使用しないので、削除しても構いません。

できたら、migrateコマンドを打ってテーブルを作成します。

$php artisan migrate
Migrating: 2019_04_03_084022_create_examples_table
Migrated:  2019_04_03_084022_create_examples_table

が出てきたら成功です。

サイトを見てみよう

もう一つterminalを開いて自分が作成したフォルダまで移動して以下のコマンドを打つ。

$php artisan serve
Laravel development server started: <http://127.0.0.1:8000>

と出てくるので、ブラウザでhttp://localhost:8000にアクセスする。
スクリーンショット 2019-04-03 17.59.11.png

このページが見れたらOK!

(後編へ続く)

2
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
2
0