Installation Add the package to your Laravel project via Composer (note: this bundle is Symfony-based, so adaptation is required):
composer require ais/kabupatenbundle:dev-master
Since Laravel doesn’t use bundles, you’ll need to manually extract and adapt the bundle’s logic (e.g., Kabupaten entity, repository, and API routes) into Laravel’s Eloquent models, repositories, and route definitions.
Extract Core Logic
Kabupaten entity (likely in Ais/KabupatenBundle/Entity/Kabupaten.php) into app/Models/Kabupaten.php.// app/Models/Kabupaten.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Kabupaten extends Model
{
protected $table = 'kabupatens';
protected $fillable = ['name', 'province_id', 'code'];
}
Set Up Database
Run migrations to create the kabupatens table (adapt the schema from the bundle’s Doctrine migrations if needed):
php artisan make:migration create_kabupatens_table
Example migration:
Schema::create('kabupatens', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('code')->unique();
$table->unsignedBigInteger('province_id');
$table->timestamps();
});
Seed Data
Import the bundle’s sample data (e.g., from Ais/KabupatenBundle/Resources/data/kabupatens.csv) into your Laravel database:
php artisan make:seeder KabupatenSeeder
Example seeder:
public function run()
{
$csv = Storage::disk('local')->get('path/to/kabupatens.csv');
array_map('str_getcsv', explode("\n", $csv));
// Process CSV and create Kabupaten records
}
First API Endpoint Create a controller to expose the data via RESTful routes:
php artisan make:controller KabupatenController --api
Example controller:
// app/Http/Controllers/API/KabupatenController.php
namespace App\Http\Controllers\API;
use App\Models\Kabupaten;
use App\Http\Controllers\Controller;
class KabupatenController extends Controller
{
public function index()
{
return response()->json(Kabupaten::all());
}
}
Add the route in routes/api.php:
Route::get('/kabupatens', [\App\Http\Controllers\API\KabupatenController::class, 'index']);
Test the API
Visit http://your-app.test/api/kabupatens to fetch the list of kabupatens.
Data Fetching and Filtering
// Get kabupatens by province_id
$kabupatens = Kabupaten::where('province_id', $provinceId)->get();
// Search by name (partial match)
$kabupatens = Kabupaten::where('name', 'like', '%search%')->get();
$kabupatens = Kabupaten::paginate(15);
API Documentation
NelmioApiDocBundle, manually document your Laravel API using tools like:
darkaonline/l5-swagger.darkaonline/l5-swagger):
/**
* @OA\Get(
* path="/api/kabupatens",
* summary="Get all kabupatens",
* @OA\Response(response="200", description="List of kabupatens")
* )
*/
public function index()
Integration with Forms
<!-- Example: Select province and kabupaten in a form -->
<select name="province" id="province">
@foreach($provinces as $province)
<option value="{{ $province->id }}">{{ $province->name }}</option>
@endforeach
</select>
<select name="kabupaten" id="kabupaten" disabled>
<!-- Dynamically populated via JavaScript/AJAX -->
</select>
<script>
$('#province').change(function() {
const provinceId = $(this).val();
$.get(`/api/kabupatens?province_id=${provinceId}`, function(kabupatens) {
$('#kabupaten').empty();
kabupatens.forEach(k => {
$('#kabupaten').append(`<option value="${k.id}">${k.name}</option>`);
});
$('#kabupaten').prop('disabled', false);
});
});
</script>
Validation
php artisan make:request StoreKabupatenRequest
Example request:
public function rules()
{
return [
'name' => 'required|string|max:255',
'code' => 'required|string|max:10|unique:kabupatens',
'province_id' => 'required|exists:provinces,id',
];
}
Testing
KabupatenController and model:
public function test_kabupatens_index()
{
$kabupatens = Kabupaten::factory()->count(3)->create();
$response = $this->getJson('/api/kabupatens');
$response->assertStatus(200)
->assertJsonCount(3);
}
Provinces-Kabupatens Relationship:
Create a Province model and establish a many-to-one relationship:
// app/Models/Province.php
public function kabupatens()
{
return $this->hasMany(Kabupaten::class);
}
// app/Models/Kabupaten.php
public function province()
{
return $this->belongsTo(Province::class);
}
Caching: Cache frequently accessed kabupaten lists to reduce database load:
$kabupatens = Cache::remember('kabupatens_all', now()->addHours(1), function() {
return Kabupaten::all();
});
Localization: If kabupaten names are locale-specific, use Laravel’s localization features:
// app/Models/Kabupaten.php
public function getNameAttribute()
{
return __("kabupatens.{$this->code}");
}
Define translations in resources/lang/id/kabupatens.php:
return [
'3676' => 'Kota Bandung',
'3671' => 'Bandung',
];
Symfony vs. Laravel Incompatibilities
routes/api.php or use Route::resource() for RESTful conventions.Missing Dependencies
NelmioApiDocBundle, FOSRestBundle, and JMSSerializerBundle, which are Symfony-specific. Laravel alternatives:
darkaonline/l5-swagger or spatie/laravel-api-docs.Data Format Mismatches
$csv = Str::of(file_get_contents('path/to/kabupatens.csv'))
->replace([';',
How can I help you explore Laravel packages today?