Setup database in Laravel Lumen micro-framework

by Pete Houston

Since Lumen is so called a “micro-”, so the part relates to database is stripped off by default. Developers are free to pick up any strategy that fits their works.

Okay, so for this article, I will give you a simple guide to work with database in Laravel Lumen framework.

Be sure to know the location of Lumen database documentation. For this, I won’t tell you things that already mentioned in the documentation page, but the steps of how to config and work with database setup.

Let’s get started!

Initialize the database resources

Let us make the “database”, if you need more information about the “artisan make” command, visit my previous article.

$ php artisan make database

The command will create the parent “database” directory including its two-children “migrations” and “seeds”.

Create the migrations

Let say, we are trying to build the schema for “users” table.

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

A migration file will be generated under “database/migrations”, it will look like this.

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)
* Reverse the migrations.
* @return void
public function down()

Config the database settings

This part is tricky, by default, Lumen is not trying to load the “.env” file in your project. You have to tell Lumen to do it. For this, open up “bootstrap/app.php”, and un-comment the following line:


After this, the config options in “.env” will be loaded automatically. Example of my configuration


Enable the facades

Up to this point, you will think that the migration will be executed perfectly. Well, I think it won’t. If you try to issue the commands,

$ php artisan migrate

It will say errors.

PHP Fatal error: Class ‘Schema’ not found in /Users/petehouston/Development/projects/tutorials/another-lumen/database/migrations/2015_04_15_084503_create_users_table.php on line 15

The reason is that, “Schema” is a facade, and once again, facade is disabled by default. We need to enable it, by un-commenting the following line in the “bootstrap/app.php”.


Trying to execute migration again, it will work!

Config the class mapping

Running the migration has been done successfully. Okay, let’s try with the rollback call.

$ php artisan migrate:rollback

Something bugs out of the project.

PHP Fatal error: Class ‘CreateUsersTable’ not found in /Users/petehouston/Development/projects/tutorials/another-lumen/vendor/illuminate/database/Migrations/Migrator.php on line 301

Oh, the traditional class-not-found error, let’s try the Composer then.

$ composer dump-autoload

Try to rollback again, it still won’t work. Why? The reason is very simple, as mentioned above, the part of database is stripped off, so it is not autoloaded in composer configuration, that is, “composer.json”. Therefore, we need to add the “classmap” key to the database directory.

Update the “autoload” section in “composer.json” like the following:

“autoload”: {
“classmap”: [
“psr-4”: {
“App\\”: “app/”

Just dump-autoload again, and execute the rollback command. It works now!

Alright, the database will work properly and exactly like Laravel 5 from this point towards. You can try to interact with database using either “DB” facade, or “Eloquent ORM”, or adding any ORM, DAO of your choice.

Have fun and happy Lumen-ing!