Lumen: API Development – Episode II ?>

Lumen: API Development – Episode II

In this episode, we will configure the environment of the API, and setup some fake data. For those not yet read the Episode I, you can read it here.

In general, we have these steps:

  1. Configure .env
  2. Create model
  3. Create migration script & do migration
  4. Create seed script & do DB Seeding

For the first few steps will straightforward before we continue with Eloquent.

  1. Rename .env.example to .env
  2. Generate 32 random characters and update the APP_KEY – I use textmechanic to generate random characters
  3. Next, open up bootstrap/app.php and uncomment the following lines:
    • Line 6 – to enable to load .env file
    • Line 25 – to enable Eloquent

Now we’re done with minimum requirement configuration for our API. Next, we going to setup our fake data.

Before proceed to the next section, if you noticed that I did not change much in .env. This is due we’re using homestead – everything is ready for our development, including the database. So, no need to further change the configuration file .env, unless we’re moving to production, you may want to configure for production.

For our fake data, I have a table called departments. The departments table have the id, name and ordering.

In this section, we will learn about Model, Migration and Seeds.

First, create a model for departments and save it in app directory.

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Department extends Model
	// for security concern, only allow these fields for mass assignments
	//
	protected $fillable = ['id','name','ordering'];

	// limit attributes displayed in model's array or JSON
	protected $hidden = ['created_at','updated_at'];

Next, we need to create Migration scripts for departments table. Run the following in Git Bash.

php artisan make:migration create_departments_table --create=departments

And open up the database/migrations/[date-time]_create_departments_table.php and update the up method as following. This will preparing our departments table schema.

<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateDepartmentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('departments', function (Blueprint $table) { $table->increments('id');

     * Reverse the migrations.
     * @return void
    public function down()

Now connect to homestead via vagrant ssh and then run the migration script

php artisan migrate

Next, we need to add some fake data using the faker. Open up database/factories/ModelFactory.php and paste in the following codes in it:

<?php $factory->define(App\Department::class, function ($faker) {
    return [
        'name' => $faker->name,
        'ordering' => mt_rand(1,50)

[Update 20/1/2016]
Next, to insert our data, open up database/seeds/DatabaseSeeders.php and add the following lines in method run.


use Illuminate\Database\Seeder;

// include the model
use App\Department;

class DatabaseSeeder extends Seeder
     * Run the database seeds.
     * @return void
    public function run()
    	DB::statement('SET FOREIGN_KEY_CHECKS = 0');

    	// empty table first

    	// create 50 records
    	factory(Department::class, 50)->create();

        // $this->call('UserTableSeeder');

Now enter the vagrant ssh and go to document root of the API, then run php artisan db:seed. You may verify your data by login to MySQL.

So, that’s done for preparing our data, using some fake data. Next episode will be talking about Route & Controller. Stay tuned for next episode!

Leave a Reply

Your email address will not be published. Required fields are marked *

18 − five =