| Install | |
|---|---|
composer require yahaaylabs/laravel-barangay-search |
|
| Latest Version: | v0.1.4 |
| PHP: | ^8.1 |
A Laravel Livewire component for searching Philippine Barangays with optional Mary UI support. This package uses the GIS.PH SDK to interact with the official GIS.PH API.
composer require yahaaylabs/laravel-barangay-search
php artisan vendor:publish --tag=barangay-search-config
If you want to customize the views:
php artisan vendor:publish --tag=barangay-search-views
Add your GIS.PH API key to your .env file:
GISPH_API_KEY=your_api_key_here
Get your API key from https://gis.ph
<livewire:barangay-search
wire:model="selectedBarangay"
label="Select Barangay"
placeholder="Search for a barangay..."
/>
<x-form wire:submit="save">
<x-input label="Name" wire:model="name" />
<livewire:barangay-search
wire:model="form.barangay"
label="Barangay"
:required="true"
hint="Start typing to search"
/>
<x-slot:actions>
<x-button label="Cancel" link="/dashboard" />
<x-button label="Save" type="submit" spinner="save" />
</x-slot:actions>
</x-form>
<livewire:barangay-search wire:model="barangay" />
@script
<script>
$wire.on('barangay-selected', (event) => {
console.log('Selected:', event.barangay);
// Do something with the selected barangay
});
$wire.on('barangay-cleared', () => {
console.log('Selection cleared');
});
$wire.on('barangay-search-error', (event) => {
console.error('Search error:', event.error);
});
</script>
@endscript
| Prop | Type | Default | Description |
|---|---|---|---|
wire:model |
mixed | null |
Bind the selected barangay |
label |
string | '' |
Label text above the input |
placeholder |
string | Config value | Placeholder text for the input |
required |
boolean | false |
Mark field as required |
clearable |
boolean | true |
Show clear button |
hint |
string | '' |
Helper text below the input |
municipalityCode |
string | null |
Filter results by municipality code |
cityCode |
string | null |
Filter results by city code |
provinceCode |
string | null |
Filter results by province code |
containerClass |
string | '' |
Additional CSS classes for container |
inputClass |
string | '' |
Additional CSS classes for input |
<livewire:barangay-search
wire:model="barangay"
:municipality-code="$selectedMunicipalityCode"
label="Select Barangay"
/>
<livewire:barangay-search
wire:model="barangay"
:province-code="$selectedProvinceCode"
label="Select Barangay in {{ $provinceName }}"
/>
<livewire:barangay-search
wire:model="barangay"
container-class="my-custom-class"
input-class="custom-input-style"
/>
If you don't want to use Mary UI styling, update your config:
// config/barangay-search.php
'ui' => [
'use_mary_ui' => false, // Use vanilla styling
],
The configuration file config/barangay-search.php provides several customization options:
return [
'api_key' => env('GISPH_API_KEY'),
'cache' => [
'enabled' => true,
'ttl' => 3600, // 1 hour
'prefix' => 'barangay_search',
],
'search' => [
'min_query_length' => 2,
'debounce_ms' => 300,
'max_results' => 20,
],
'ui' => [
'placeholder' => 'Search for a barangay...',
'no_results_text' => 'No barangays found',
'loading_text' => 'Searching...',
'use_mary_ui' => true,
],
];
You can also use the BarangayService directly in your controllers or other classes:
use YahaayLabs\LaravelBarangaySearch\Services\BarangayService;
class YourController extends Controller
{
public function __construct(
protected BarangayService $barangayService
) {}
public function search(Request $request)
{
$results = $this->barangayService->search(
query: $request->input('q'),
filters: [
'municipality_code' => $request->input('municipality_code')
]
);
return response()->json($results);
}
public function getByCode(string $code)
{
$barangay = $this->barangayService->getByCode($code);
return response()->json($barangay);
}
public function getByMunicipality(string $municipalityCode)
{
$barangays = $this->barangayService->getByMunicipality($municipalityCode);
return response()->json($barangays);
}
}
When a barangay is selected, the component returns an array with the following structure:
[
'code' => '012345678',
'name' => 'Barangay Name',
'municipality' => 'Municipality Name',
'municipality_code' => '012345',
'city' => 'City Name', // nullable
'city_code' => '012345', // nullable
'province' => 'Province Name',
'province_code' => '0123',
'region' => 'Region Name',
'region_code' => '01',
'full_address' => 'Barangay Name, Municipality Name, Province Name',
]
The component dispatches the following events:
barangay-selectedFired when a barangay is selected from the dropdown.
$wire.on('barangay-selected', (event) => {
console.log(event.barangay); // Selected barangay object
});
barangay-clearedFired when the selection is cleared.
$wire.on('barangay-cleared', () => {
// Handle clear action
});
barangay-search-errorFired when a search error occurs.
$wire.on('barangay-search-error', (event) => {
console.error(event.error); // Error message
});
The package includes built-in caching to reduce API calls:
// Clear all cached searches
use YahaayLabs\LaravelBarangaySearch\Services\BarangayService;
app(BarangayService::class)->clearCache();
composer test
Please see CHANGELOG for more information on what has changed recently.
Contributions are welcome! Please feel free to submit a Pull Request.
If you discover any security-related issues, please email security@yahaaylabs.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
For support, email support@yahaaylabs.com or visit our documentation.