1. Go to this page and download the library: Download awalhadi/addressable 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/ */
awalhadi / addressable example snippets
namespace App\Models;
use Awalhadi\Addressable\Traits\Addressable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use Addressable;
// Your existing model code...
}
// Get all addresses
$addresses = $user->addresses;
// Get primary address
$primaryAddress = $user->primaryAddress();
// Get addresses by type
$homeAddresses = $user->addresses()->ofType('home')->get();
$billingAddresses = $user->addresses()->isBilling()->get();
// Get addresses within radius
$nearbyAddresses = $user->addresses()
->withCoordinates()
->get()
->filter(fn($address) => $address->isWithinRadius(40.7128, -74.0060, 10));
// Get full name
$address->full_name; // "John Doe"
// Get formatted address
$address->full_address; // "123 Main Street, Apt 4B, New York, NY 10001, US"
// Get country name
$address->country_name; // "United States"
// Get formatted phone
$address->formatted_phone; // "(555) 123-4567"
// Get masked phone (for privacy)
$address->masked_phone; // "(555) ***-4567"
// Get masked email (for privacy)
$address->masked_email; // "j***@example.com"
// Filter by type
Address::ofType('home')->get();
// Filter by country
Address::inCountry('US')->get();
// Filter by city
Address::inCity('New York')->get();
// Filter by state
Address::inState('NY')->get();
// Filter by postal code
Address::inPostalCode('10001')->get();
// Only verified addresses
Address::isVerified()->get();
// Only addresses with coordinates
Address::withCoordinates()->get();
// Recent addresses (last 30 days)
Address::recent()->get();
// Get all addresses
$user->addresses;
// Get primary address
$user->primaryAddress();
// Get billing address
$user->billingAddress();
// Get shipping address
$user->shippingAddress();
// Check if has addresses
$user->hasAddresses();
// Check if has primary address
$user->hasPrimaryAddress();
// Get addresses by type
$user->getAddressesByType('home');
// Get addresses in country
$user->getAddressesInCountry('US');
// Get addresses within radius
$user->getAddressesWithinRadius($lat, $lng, $radius);
// Create multiple addresses
$user->createManyAddresses([
['type' => 'home', 'street' => '123 Home St'],
['type' => 'work', 'street' => '456 Work Ave'],
]);
// Update multiple addresses
$user->updateManyAddresses([
'home' => ['street' => '789 New Home St'],
'work' => ['street' => '012 New Work Ave'],
]);
// Calculate distance between two addresses
$distance = $address1->distanceTo($address2);
// Check if address is within radius
$isNearby = $address->isWithinRadius($lat, $lng, 10);
// Calculate distance using Haversine formula
$distance = $address->calculateDistance($lat, $lng);
// Calculate distance using Vincenty formula (more accurate)
$distance = $address->calculateDistanceVincenty($lat, $lng);
// Check if point is in polygon (geofencing)
$isInside = $address->isPointInPolygon($polygon);
// Create bounding box
$bbox = $address->createBoundingBox($radius);
// Convert decimal to DMS format
$dms = $address->decimalToDMS($latitude);
// Convert DMS to decimal
$decimal = $address->dmsToDecimal($dms);
// Calculate midpoint between two coordinates
$midpoint = $address->calculateMidpoint($lat1, $lng1, $lat2, $lng2);
// Validate entire address
$isValid = $address->isValid();
// Get validation errors
$errors = $address->getValidationErrors();
// Validate postal code
$isValid = $address->validatePostalCode();
// Validate phone number
$isValid = $address->validatePhoneNumber();
// Validate email
$isValid = $address->validateEmail();
// Validate country code
$isValid = $address->validateCountryCode();
// Format postal code
$formatted = $address->formatPostalCode();
// Format phone number
$formatted = $address->formatPhoneNumber();
// Geocode address (get coordinates)
$address->geocode();
// Reverse geocode (get address from coordinates)
$address->reverseGeocode();
// Check if address has coordinates
$hasCoords = $address->hasCoordinates();
// Check if address is complete
$isComplete = $address->isComplete();
// Cache address data
$address->cacheAddressData();
// Get cached address data
$cached = $address->getCachedAddressData();
// Clear address cache
$address->clearAddressCache();
// Cache geocoding results
$address->cacheGeocodingResult($result);
// Get cached geocoding result
$cached = $address->getCachedGeocodingResult();
// Clear all related caches
$address->clearAllRelatedCaches();
// Warm cache for addressable
$user->warmAddressCache();