Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

akaydin's avatar
Level 20

Address & Location Library

Hey guys,

I need an address book for the customers, carriers and the company (which the user represents) models. At first, a trait like Addressable might be a good path. But when consider the fact that 99% of the addressbook will be the customers; I think keeping everything on the parent model is the best way. Looking for your recommendations.

The second , do you have any suggestions for the addressbook library? I mean, countries, cities, telephone codes, maybe the currencies.. No need to re-invent. I tried to find some but most of them are older than 5 years, and also many are for a specific country. Is there a global standard for these? Or any kind of service like google, amazon or maybe laravel?

Thanks in advance. Alpay

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Hi Alpay,

For your address book requirements, you have a couple of options to consider. Let's break it down into two parts: the structure of your address book and the libraries/services you can use for global data like countries, cities, and telephone codes.

1. Address Book Structure

Given that 99% of the address book will be for customers, it makes sense to keep the address information within the parent model. However, using a trait like Addressable can still be beneficial for code reusability and separation of concerns. Here's a simple example of how you can implement this:

Addressable Trait

namespace App\Traits;

use App\Models\Address;

trait Addressable
{
    public function addresses()
    {
        return $this->morphMany(Address::class, 'addressable');
    }

    public function addAddress($addressData)
    {
        return $this->addresses()->create($addressData);
    }
}

Address Model

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Address extends Model
{
    protected $fillable = ['address_line_1', 'address_line_2', 'city', 'state', 'country', 'postal_code', 'phone'];

    public function addressable()
    {
        return $this->morphTo();
    }
}

Customer Model

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Traits\Addressable;

class Customer extends Model
{
    use Addressable;

    // Other customer-specific code
}

This way, you can easily add addresses to any model that uses the Addressable trait.

2. Address Book Library

For global data like countries, cities, and telephone codes, you can use existing libraries or services. Here are a few recommendations:

Libraries

  1. Laravel-Intl: This package provides a simple way to handle internationalization, including countries and currencies.

  2. World Countries: A package that provides a list of countries, states, and cities.

Services

  1. Google Places API: Provides detailed information about places, including addresses and phone numbers.

  2. Geonames: A geographical database that covers all countries and contains over eleven million placenames.

  3. REST Countries: A free API for getting information about countries, including names, codes, and currencies.

Example Usage of REST Countries API

Here's a simple example of how you can fetch country data using the REST Countries API in Laravel:

use Illuminate\Support\Facades\Http;

$response = Http::get('https://restcountries.com/v3.1/all');

$countries = $response->json();

// Example: Print the name and currency of each country
foreach ($countries as $country) {
    echo $country['name']['common'] . ' - ' . $country['currencies'][0]['name'] . PHP_EOL;
}

By combining these approaches, you can create a robust and scalable address book system for your application.

I hope this helps! Let me know if you have any further questions.

Best, LaracastsGPT

1 like

Please or to participate in this conversation.