1. Go to this page and download the library: Download dev-giri/pin-india library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dev-giri / pin-india example snippets
return [
// Your data.gov.in API key for downloading postal data
'data_gov_in_api_key' => env('DATA_GOV_IN_API_KEY'),
// Prefix for database tables (helps avoid conflicts)
'table_prefix' => env('PININDIA_TABLE_PREFIX', 'pinindia'),
// Local storage path for downloaded postal data
'data_path' => env('PININDIA_DATA_PATH', 'pinindia/post_offices.json'),
];
use PinIndia\Facades\PinIndia;
// Find post offices by pincode
$postOffices = PinIndia::findByPincode(110001);
// Find post offices by name
$postOffices = PinIndia::findByPostOffice('Delhi');
// Find nearest post offices by coordinates
$postOffices = PinIndia::getNearestByCoordinates(28.6139, 77.2090, 5); // lat, long, radius in km
// Find nearest post offices by pincode
$postOffices = PinIndia::getNearestByPincode(110001, 5); // pincode, radius in km
// Find nearest post offices by post office name
$postOffices = PinIndia::getNearestByPostOffice('Delhi GPO', 5); // name, radius in km
use PinIndia\Models\State;
use PinIndia\Models\District;
// Autocomplete states
public function autocompleteStates(Request $request)
{
$query = $request->input('query');
$states = State::where('name', 'like', "%{$query}%")
->orderBy('name')
->limit(10)
->get()
->map(function($state) {
return [
'id' => $state->id,
'name' => $state->name
];
});
return response()->json($states);
}
// Autocomplete districts within a state
public function autocompleteDistricts(Request $request)
{
$query = $request->input('query');
$stateId = $request->input('state_id');
$districts = District::when($stateId, function($q) use ($stateId) {
return $q->where('state_id', $stateId);
})
->where('name', 'like', "%{$query}%")
->orderBy('name')
->limit(10)
->get()
->map(function($district) {
return [
'id' => $district->id,
'name' => $district->name,
'state_id' => $district->state_id,
'state_name' => $district->state->name
];
});
return response()->json($districts);
}
use PinIndia\Models\State;
use PinIndia\Models\District;
use PinIndia\Models\Pincode;
use PinIndia\Models\PostOffice;
// Get all states for the first dropdown
public function getStates()
{
$states = State::orderBy('name')->get();
return response()->json($states);
}
// Get districts based on selected state
public function getDistricts($stateId)
{
$districts = District::where('state_id', $stateId)
->orderBy('name')
->get();
return response()->json($districts);
}
// Get pincodes based on selected district
public function getPincodes($districtId)
{
$pincodes = Pincode::where('district_id', $districtId)
->orderBy('pincode')
->get()
->pluck('pincode')
->unique()
->values();
return response()->json($pincodes);
}
// Get post offices based on selected pincode
public function getPostOffices($pincode)
{
$postOffices = PostOffice::whereHas('pincode', function($query) use ($pincode) {
$query->where('pincode', $pincode);
})
->orderBy('name')
->get(['id', 'name', 'office']);
return response()->json($postOffices);
}
// Get user's current location from browser/device
$latitude = $request->input('latitude');
$longitude = $request->input('longitude');
$radius = 2; // 2 km radius
// Find nearest post offices
$nearestPostOffices = PinIndia::getNearestByCoordinates($latitude, $longitude, $radius);
// First result is likely the user's current postal area
$currentLocation = $nearestPostOffices->first();
// Display address information
echo "Your current area: {$currentLocation->name}, {$currentLocation->district}, {$currentLocation->state} - {$currentLocation->pincode}";
// User starts typing post office name
$query = $request->input('query'); // e.g., "Andheri"
// Search for matching post offices
$suggestions = PinIndia::findByPostOffice($query, 5); // Limit to 5 results
return response()->json([
'suggestions' => $suggestions->map(function($po) {
return [
'label' => "{$po->name}, {$po->district}, {$po->state} - {$po->pincode}",
'value' => $po->pincode
];
})
]);
// Customer's pincode
$customerPincode = $request->input('pincode');
// Your warehouse/store location
$warehousePincode = 400001; // Mumbai GPO
$maxDeliveryRadius = 25; // 25 km delivery radius
// Find distance between warehouse and customer
$nearestPostOffices = PinIndia::getNearestByPincode($warehousePincode, $maxDeliveryRadius);
// Check if customer's pincode is in delivery range
$isDeliverable = $nearestPostOffices->contains(function($postOffice) use ($customerPincode) {
return $postOffice->pincode == $customerPincode;
});
if ($isDeliverable) {
echo "Delivery available to your location!";
} else {
echo "Sorry, we don't deliver to your area yet.";
}
use PinIndia\Models\State;
use PinIndia\Models\District;
// Count post offices by state
$stateStats = State::withCount('districts.pincodes.postOffices')
->get()
->map(function($state) {
return [
'state' => $state->name,
'post_office_count' => $state->districts_pincodes_post_offices_count
];
})
->sortByDesc('post_office_count');
// Find districts with highest post office density
$districtStats = District::withCount('pincodes.postOffices')
->with('state')
->get()
->map(function($district) {
return [
'district' => $district->name,
'state' => $district->state->name,
'post_office_count' => $district->pincodes_post_offices_count
];
})
->sortByDesc('post_office_count')
->take(10);
bash
# Install the package
php artisan pinindia:install
# Download post office data from data.gov.in
php artisan pinindia:download
# Uninstall the package
php artisan pinindia:uninstall
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.