The Blog

My Toughts

Nested dropdown implementing indonesia region

Nested dropdown implementing indonesia region

chained combobox/dropdown  yang saling terkait atau berantai. Contoh paling sering digunakan adalah pemilihan provinsi pada form. Di mana, ketika provinsi dipilih, maka akan menampilkan kota/kabupaten yang ada dalam provinsi tersebut. Hal yang sama juga terjadi ketika kita memilih kota/kabupaten tertentu, maka akan menampilkan daftar kecamatan pada kota/kabupaten terpilih.

 

Prepare migtations

create your provinces migration, your file should like this.

// 2017_05_02_140432_create_provinces_tables.php

<?php

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

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

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

after create your provinces migration. now create your regencies migration, your file should like this.

// 2017_05_02_140444_create_regencies_tables.php

<?php

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

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

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

after create your regencies migration. now create your district migration, your file should like this.

// 2017_05_02_140450_create_districts_tables.php
<?php

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

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

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

after create your districts migration. now create your villages migration, your file should like this.

// 2017_05_02_140454_create_villages_tables.php

<?php

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

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

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

 

Prepare models

// App/Models/Region/Province.php

<?php

/*
 *
 * (c) NicoAudy <nicoaudy1994@gmail.com>
 *
 */

namespace App\Models\Region;

use App\Models\Region\Regency;
use Illuminate\Database\Eloquent\Model;

class Province extends Model
{
    public function regencies()
    {
        return $this->hasMany(Regency::class);
    }
}
// App/Models/Region/Regency.php

<?php

/*
 *
 * (c) NicoAudy <nicoaudy1994@gmail.com>
 *
 */

namespace App\Models\Region;

use App\Models\Region\Province;
use App\Models\Region\District;
use Illuminate\Database\Eloquent\Model;

class Regency extends Model
{
    public function province()
    {
        return $this->belongsTo(Province::class);
    }

    public function districts()
    {
        return $this->hasMany(District::class);
    }
}
// App/Models/Region/District
<?php

/*
 *
 * (c) NicoAudy <nicoaudy1994@gmail.com>
 *
 */

namespace App\Models\Region;

use App\Models\Region\Regency;
use App\Models\Region\Village;
use Illuminate\Database\Eloquent\Model;

class District extends Model
{
    public function regency()
    {
        return $this->belongsTo(Regency::class);
    }

    public function villages()
    {
        return $this->hasMany(Village::class);
    }
}
// App/Models/Region/Village.php

<?php

/*
 *
 * (c) NicoAudy <nicoaudy1994@gmail.com>
 *
 */

namespace App\Models\Region;

use App\Models\Region\District;
use Illuminate\Database\Eloquent\Model;

class Village extends Model
{
  public function district()
  {
      return $this->belongsTo(District::class);
  }
}

Prepare seeders

// IndoRegionProvinceSeeder.php

<?php

use Illuminate\Database\Seeder;

class IndoRegionProvinceSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @deprecated
     *
     * @return void
     */
    public function run()
    {
        $file = file_get_contents(database_path('seeds/data/provinces.txt'));
        $provinces = unserialize($file);
        DB::table('provinces')->insert($provinces);
    }
}
// IndoRegionRegencySeeder.php

<?php

use Illuminate\Database\Seeder;

class IndoRegionRegencySeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @deprecated
     *
     * @return void
     */
    public function run()
    {
        $file = file_get_contents(database_path('seeds/data/regencies.txt'));
        $regencies = unserialize($file);
        DB::table('regencies')->insert($regencies);
    }
}
// IndoRegionDistrictSeeder.php

<?php

use Illuminate\Database\Seeder;

class IndoRegionDistrictSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @deprecated
     *
     * @return void
     */
    public function run()
    {
        $file = file_get_contents(database_path('seeds/data/districts.txt'));
        $districts = unserialize($file);
        DB::table('districts')->insert($districts);
    }
}
// IndoRegionVillageSeeder.php

<?php

use Illuminate\Database\Seeder;

class IndoRegionVillageSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @deprecated
     *
     * @return void
     */
     public function run()
     {
        $file = file_get_contents(database_path('seeds/data/villages.txt'));
        $villages = unserialize($file);
        foreach ($villages as $village) {
            DB::table('villages')->insert($village);
        }
    }
}

 

and use click link to get the data . thankyou


- 3/4 -