LoginSignup
1
1

More than 5 years have passed since last update.

Lumen5.6でSQLiteを使う

Last updated at Posted at 2018-09-09

さくらのレンタルサーバーのライトで契約している趣味のサイトで、やっぱりDB使いたいな……となったので、とりえあず Lumen で SQLite を使えるようにしてみる。自分用メモ。

環境

  • vagrant
  • Lumen5.6
  • SQLite
  • (PHPStorm)

手順

1)database/database.sqlite を作る

アプリの根っこにある database ディレクトリに、SQLite 用のファイルを作る。

$ touch database/database.sqlite

2).env で DB の設定

vagrant とローカルの共有ディレクトリ使ってる場合は、vagrant 上での絶対パスを書く。

DB_CONNECTION=sqlite
DB_DATABASE=/home/vagrant/common/lumen-sqlite.net/database/database.sqlite

3)Facade と ORM 使う場合はコメントアウトを外す

公式にも書いてますが、
bootstrap/app.php
を少し変更しないと、Facade と ORM が使えないので、設定変更。

# bootstrap/app.php
.
.
.
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

$app = new Laravel\Lumen\Application(
    realpath(__DIR__.'/../')
);

$app->withFacades(); // <- この人と
$app->withEloquent(); // <- この人をコメントインさせる
.
.
.

3.5) 余談。

ORM使いたいのに↑をコメントインしていないと、
スクリーンショット 2018-09-09 16.16.11.png

 (1/1) Error
Call to a member function connection() on null

と言って怒られる。

4)DB 作成

$ php artisan make:migration create_users_table --create=users

---(中身)---
<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

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

---(中身ここまで)---

$ php artisan migrate

5)中身確認

いろんなツールあるようなので、自分に合ったものを使うとよいかと。
私は PHPStorm 使ってましたので、公式にある方法で PHPStorm 内で SQLite の DB 見られるようにしました。

▼PhpStorm2018.2 公式ヘルプ
 https://pleiades.io/help/phpstorm/connecting-to-a-database.html#sqlite

参考

▼Lumen本家 Database Basic Usage
 https://lumen.laravel.com/docs/5.2/database#basic-usage

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