PHPackages                             hasyirin/laravel-address - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. hasyirin/laravel-address

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hasyirin/laravel-address
========================

This is my package laravel-address

v2.3.0(3mo ago)056[2 PRs](https://github.com/hasyirin/laravel-address/pulls)MITPHPPHP ^8.4CI passing

Since Feb 8Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/hasyirin/laravel-address)[ Packagist](https://packagist.org/packages/hasyirin/laravel-address)[ Docs](https://github.com/hasyirin/laravel-address)[ GitHub Sponsors]()[ RSS](/packages/hasyirin-laravel-address/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (12)Versions (10)Used By (0)

Laravel Address
===============

[](#laravel-address)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c364833c8eec5244316860521ea8601c22b94ae0ff896c1749f3aef66ca33da3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f686173796972696e2f6c61726176656c2d616464726573732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hasyirin/laravel-address)[![GitHub Tests Action Status](https://camo.githubusercontent.com/5131b3023712d3ee68f27e820252c518cc528f426004778167efee417cff37dc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f686173796972696e2f6c61726176656c2d616464726573732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/hasyirin/laravel-address/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/855c7164170f08c2747ac344046f159f9ff9f3a54e27972c0abdf96379944521/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f686173796972696e2f6c61726176656c2d616464726573732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/hasyirin/laravel-address/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/9724b6ac47ce982910e7e1de4d33ce3ca3922c78e3c8e6736f44193ccff20740/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686173796972696e2f6c61726176656c2d616464726573732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hasyirin/laravel-address)

A Laravel package for managing addresses with polymorphic relationships and hierarchical geographical data (countries, states, districts, subdistricts, and post offices).

Comes with built-in geographical data for Malaysia and a seeder command to populate reference tables.

Requirements
------------

[](#requirements)

- PHP 8.4+
- Laravel 11 or 12

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require hasyirin/laravel-address
```

Publish and run the migrations:

```
php artisan vendor:publish --tag="laravel-address-migrations"
php artisan migrate
```

Optionally publish the config file:

```
php artisan vendor:publish --tag="laravel-address-config"
```

Seeding Geographical Data
-------------------------

[](#seeding-geographical-data)

Seed countries, states, districts, subdistricts, and post offices:

```
php artisan address:seed
```

The command loads data from the package's `data/` directory. To use your own data, place JSON files in your application's `base_path('data')` directory following the same structure.

Usage
-----

[](#usage)

### Preparing Your Model

[](#preparing-your-model)

Add the `InteractsWithAddresses` trait to any model that should have addresses:

```
use Hasyirin\Address\Concerns\InteractsWithAddresses;
use Hasyirin\Address\Contracts\Addressable;

class User extends Model implements Addressable
{
    use InteractsWithAddresses;
}
```

### Creating Addresses

[](#creating-addresses)

```
$user->address()->create([
    'type' => 'primary',
    'line_1' => '123 Main Street',
    'line_2' => 'Suite 4B',
    'line_3' => 'Taman Example',
    'postcode' => '50000',
    'country_id' => $country->id,
    'state_id' => $state->id,
    'post_office_id' => $postOffice->id,
    'latitude' => 3.1390,
    'longitude' => 101.6869,
    'properties' => ['notes' => 'Front gate access'],
]);
```

### Using the Factory

[](#using-the-factory)

The `Address` model includes a factory for testing:

```
use Hasyirin\Address\Models\Address;

Address::factory()
    ->for($user, 'addressable')
    ->create([
        'type' => 'primary',
        'line_1' => 'No. 1, Jalan Test',
        'postcode' => '40000',
    ]);
```

### Retrieving Addresses

[](#retrieving-addresses)

```
// Primary address (returns a default empty instance if none exists)
$user->address;

// All addresses
$user->addresses;

// Address by type
$user->getAddress('billing');
```

### Querying by Type

[](#querying-by-type)

The `ofType` scope accepts a string, array, or enum:

```
use Hasyirin\Address\Models\Address;

Address::ofType('shipping')->get();
Address::ofType(['billing', 'shipping'])->get();
```

### Formatting

[](#formatting)

```
// 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);
```

### HTML Rendering

[](#html-rendering)

```
// Multi-line with  tags
$address->render();

// Inline comma-separated
$address->render(inline: true);

// With options
$address->render(state: false, country: false, capitalize: true, margin: 1);
```

### Copying an Address

[](#copying-an-address)

Create an unsaved copy of an address (without the polymorphic relationship or type):

```
$copy = $address->copy();
$copy->type = 'billing';
$copy->addressable()->associate($anotherModel);
$copy->save();
```

Configuration
-------------

[](#configuration)

```
// config/address.php

return [
    // Mark a country/state as "local" during seeding
    'locality' => [
        'country' => null, // e.g. 'MYS'
        'state' => null,   // e.g. '01'
    ],

    // Override model classes
    'models' => [
        'address' => \Hasyirin\Address\Models\Address::class,
        'country' => \Hasyirin\Address\Models\Country::class,
        'state' => \Hasyirin\Address\Models\State::class,
        'district' => \Hasyirin\Address\Models\District::class,
        'subdistrict' => \Hasyirin\Address\Models\Subdistrict::class,
        'post-office' => \Hasyirin\Address\Models\PostOffice::class,
    ],

    // Override table names
    'tables' => [
        'addresses' => 'addresses',
        'countries' => 'countries',
        'states' => 'states',
        'districts' => 'districts',
        'subdistricts' => 'subdistricts',
        'post_offices' => 'post_offices',
    ],
];
```

Data Structure
--------------

[](#data-structure)

The package uses a hierarchical location model:

```
Country
 └── State
      ├── District
      │    └── Subdistrict
      └── Post Office (with postcodes)

```

Each `Address` belongs to a `Country`, `State`, and optionally a `PostOffice`, and is polymorphically attached to any model via the `addressable` morph relationship.

Models
------

[](#models)

ModelKey Fields`Country``code` (ISO 3166-1 alpha-3), `alpha_2`, `name`, `local``State``code`, `name`, `local`, belongs to `Country``District``code`, `name`, belongs to `State``Subdistrict``code`, `name`, belongs to `District``PostOffice``name`, `postcodes` (JSON array), belongs to `State``Address``type`, `line_1`-`line_3`, `postcode`, `latitude`, `longitude`, `properties` (JSON)All models use soft deletes.

Extending Models
----------------

[](#extending-models)

To use your own model classes, extend the package models and update the config:

```
use Hasyirin\Address\Models\Address as BaseAddress;

class Address extends BaseAddress
{
    // your customizations
}
```

```
// config/address.php
'models' => [
    'address' => \App\Models\Address::class,
    // ...
],
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [Hasyirin Fakhriy](https://github.com/hasyirin)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance86

Actively maintained with recent releases

Popularity8

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 85.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

5

Last Release

99d ago

Major Versions

v1.0.0 → v2.0.02026-02-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/e14b4ff4d4a5af7bcf783ff180e121b90eb3f16e57c4188286100efc43f9355c?d=identicon)[hasyirin](/maintainers/hasyirin)

---

Top Contributors

[![hasyirin](https://avatars.githubusercontent.com/u/25414222?v=4)](https://github.com/hasyirin "hasyirin (47 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")

---

Tags

laravellaravel-addressHasyirin Fakhriy

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/hasyirin-laravel-address/health.svg)

```
[![Health](https://phpackages.com/badges/hasyirin-laravel-address/health.svg)](https://phpackages.com/packages/hasyirin-laravel-address)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
