1. Go to this page and download the library: Download hasyirin/laravel-address 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/ */
hasyirin / laravel-address example snippets
use Hasyirin\Address\Concerns\InteractsWithAddresses;
use Hasyirin\Address\Contracts\Addressable;
class User extends Model implements Addressable
{
use InteractsWithAddresses;
}
use Hasyirin\Address\Models\Address;
Address::factory()
->for($user, 'addressable')
->create([
'type' => 'primary',
'line_1' => 'No. 1, Jalan Test',
'postcode' => '40000',
]);
// Primary address (returns a default empty instance if none exists)
$user->address;
// All addresses
$user->addresses;
// Address by type
$user->getAddress('billing');
use Hasyirin\Address\Models\Address;
Address::ofType('shipping')->get();
Address::ofType(['billing', 'shipping'])->get();
// Single-line comma-separated string
$address->formatted();
// "123 Main Street, Suite 4B, Taman Example, 50000, Kuala Lumpur, Selangor, Malaysia"
// Exclude state or country
$address->formatted(state: false);
$address->formatted(country: false);
// Uppercase
$address->formatted(capitalize: true);
Country::local(); // ?Country
State::local(); // ?State — matches state code AND parent country
District::local(); // ?District — matches district code AND parent state AND parent country