Nasrul Hazim Bin Mohamad

Laravel: Custom Accept API Header


Assalamualaikum / Hi,

It’s been a while since I stumble upon an API, which have custom Accept header.

For a past few days, I have a chance to implement custom header on one of my client project.

Here is how I did – create a middleware to filter the coming request.

As you can see above, my approach is using a middleware to check out every request come in. You just need to register this middleware in api middleware.

Now you’re ready with your custom Accept header.

You also can use the custom Accept header to have version control of your API.

Thanks! Happy Coding!

HAProxy: Setting Up CORS



It’s been awhile since my last post – quite tight up with business and development – and here we are, just sharing a new thing I learnt – HAProxy.

Currently I have a mobile application need to consume my API (Laravel based) – and I have no problem setting up CORS from Laravel.

But working on a bit scale up infrastructure, and in HA architecture, need to setup CORS on load balancer, which is HAProxy.

Following are the minimal setup to make API working with HAProxy.

Reset Your Password from API


As you may know, Laravel provide a scaffold auth, which include reset/forgot password on web applications side. But what if you have a mobile application that need to reset password from the mobile application?

In this post, I’ll show you how to reset user’s password from an API in Laravel.

UPDATE 02/07/2017:

In case you’re looking for App\Transformers\Json, you basically can create a class called Json in app/Transformers directory.

In the Json class:


namespace App\Transformers;

class Json
    public static function response($data = null, $message = null)
        return [
            'data'    => $data,
            'message' => $message,

Please make sure to composer dumpautoload -o afterward.

Training: Web Application Development with Lumen(API)


Date: Saturday, 6th February 2016

Available Seats: 25

Fee: Student – RM 100, Public – RM 250

Location: Level 2, above Yamaha Motorsport Shop Lot



To learn how to develop an API with micro-framework Lumen


Following items need to be install first before coming to the class:

  • Laragon
  • Git & GitHub Account
  • Sublime Text 3
  • Postman (


PHP & MySQL is compulsory. A good understanding of OOP is value added.


Software developers with PHP and MySQL background.


  • Installation
  • Routing
  • Controllers
  • Responses
  • Database
  • Migrations
  • Eloquent Queries
  • API Integration



Training: Mobile Apps Development with Intel XDK


Location: Medina Arabic Training & Solutions, Bangi Sentral (Above Melaka Kitchen Restauurant)


Date: 23th & 24th January 2016

Available Seats: 20 seats


Package Intel XDK API
2 Days 150
1 Day 50 120


Package Intel XDK API
2 Days 250
1 Day 100 175

* Fee payment must be made first in order to reserved your seats.




Register here if you unable to see the Google Form embed in this post.

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!

Lumen: API Development – Episode I


So, this post is about develop your own API using Lumen, a micro framework, a child of the Laravel (I assumed it that way). This topic will take few parts and this is the Episode 1 of Lumen: API Development.

Following tools required to be install on your machine:

  1. Virtual Box – For those using Avira Anti Virus, you might need to head over this article if you having problem with Virtual Box.
  2. Vagrant – Enable developers to easily share the same environment configurations
  3. Composer – Enable developers to manage packages for system dependencies
  4. Git – Famous version control (please do create a GitHub account)
  5. Sublime Text 3 – best text editor I’ve used so far. 🙂

Here are the basic steps taken for setting up the Lumen(all commands running in Git Bash):

  1. Create Lumen project at directory lumen-api
    composer create-project laravel/lumen lumen-api
  2. Install homestead
    cd lumen-api
    composer require laravel/homestead
  3. Make homestead
    php vendor/bin/homestead make


    php vendor/laravel/homestead/homestead make
  4. Create id_rsa at /C/Users/[yourname]/.ssh
    ssh-keygen -t rsa -C yourname@homestead

    Location of the id_rsa

    Location of the id_rsa

  5. Open Homestead.yaml and change map folder to working directory (step #1)
    map: "D:/WWW/lumen-api"

    homestead.yaml Configuration

    homestead.yaml Configuration

  6. Update C:/Windows/System32/drivers/etc/hosts file
  7. install vagrant laravel/homestead (size about 1GB..taking about 45 minutes on 4mbps connection.. T_T)
     vagrant box add laravel/homestead

    Vagrant Add Box Laravel/Homestead

    Vagrant Add Box Laravel/Homestead

  8. Open up Virtual Box & start up the vagrant
     vagrant up
  9. Try to SSH to homestead to check if it’s ok
     ssh vagrant@ -p 22

    Try to SSH to Homestead

    Try to SSH to Homestead

  10. Browse to to check if the homestead installed properly