Enable Eloquent ORM in Laravel Lumen micro-framework

Pete Houston
2 min readApr 15, 2015

by Pete Houston

In previous article, I’ve shown you the initial configuration to interact with database in Lumen.

Well, so in this article, I will guide you to add Eloquent ORM into Lumen.

Enable Eloquent ORM

You might think that since Eloquent is part of the Illuminate\Database package, so just require the package into composer.json , update and it will be done.

Well, to tell you honestly, it should not be that way, the reason is that the lumen-framework already requires that package, and include as part of its micro “Lumen\Application” instance.

To enable the Eloquent ORM for Lumen, open file “bootstrap/app.php” and un-comment the following lines:

Dotenv::load(__DIR__.’/../’);
...
$app->withFacades();
$app->withEloquent();

That’s it, you don’t have to do anything else.

Create the model

Okay, let’s make it similar to Laravel 5.

Create a new file “app/User.php” with the following code.

<?php namespace App;use Illuminate\Database\Eloquent\Model;class User extends Model {    protected $table = ‘users’;    protected $fillable = [
‘name’,
‘username’,
‘password’
];
protected $hidden = [ ‘password’ ];
}

Update the migration

Let’s update the migration that we created from previous article.

Schema::create(‘users’, function(Blueprint $table)
{
$table->increments(‘id’);
$table->string(‘name’);
$table->string(‘username’)->unique();
$table->string(‘password’);
$table->timestamps();
});

And execute it.

$ php artisan migrate

Seed the database

Let’s create a new seeder file “database/seeds/UserTableSeeder.php”, with the following code.

<?phpuse Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\User;
class UserTableSeeder extends Seeder { /**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::create([
‘name’ => ‘Pete Houston’,
‘username’ => ‘petehouston’,
‘password’ => ‘123secret’
]);
User::create([
‘name’ => ‘Taylor Otwell’,
‘username’ => ‘taylorotwell’,
‘password’ => ‘greatsecret’
]);
}}

Also, open the “database/seeds/DatabaseSeeder.php” and add the following line if not found, or un-comment if it is commented.

$this->call(‘UserTableSeeder’);

Next step is to update the “UserTableSeeder” class to the autoload.

$ composer dump-autoload

Finally, run the seed to fill the database with records.

$ php artisan db:seed

Create a route to verify

Usually, in Laravel, you can use “artisan tinker” to interact directly with Eloquent. Well, for Lumen, in a simple way, let’s create a route to verify it.

Add the following code to the “app/Http/routes.php”:

$app->get(‘/users’, function() {
return \App\User::all();
});

When hit, the routes will return all “User” records in database.

Just open server if you haven’t done it yet.

$ php artisan serve

Hit the route, if there is no mistake up to this point, you will see the JSON format return.

[{“id”: 1,”name”: “Pete Houston”,”username”: “petehouston”,”created_at”: “2015–04–15 10:13:32”,”updated_at”: “2015–04–15 10:13:32”},{“id”: 2,”name”: “Taylor Otwell”,”username”: “taylorotwell”,”created_at”: “2015–04–15 10:13:32”,”updated_at”: “2015–04–15 10:13:32”}]

As you see, use Eloquent ORM doesn’t cost you any effort. Just follow my steps exactly and things will be done.

Happy Lumen-ing ☺

--

--