Skip to content
forked from mbpcoder/world

World - PHP & Laravel Package for Geographical Data

Notifications You must be signed in to change notification settings

albertcruzacp/world

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

54 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒ World - PHP & Laravel Package for Geographical Data

World is a powerful PHP and Laravel package for retrieving structured geographical data, including continents, countries, provinces, and cities. This package is lightweight, optimized for performance, and seamlessly integrates with Laravel applications.


๐Ÿš€ Features

โœ” Get all continents, countries, provinces, and cities
โœ” Filter countries by continent
โœ” Retrieve provinces and cities for any country
โœ” Optimized for high performance with caching โœ” Compatible with Laravel and PHP
โœ” Includes migrations and seeding for easy setup
โœ” includes a Laravel Facade for simpler usage!


๐Ÿ“ฆ Installation

Install via Composer:

composer require thecoder/world

If auto-discovery is not working, manually add the service provider in config/app.php:

TheCoder\World\WorldServiceProvider::class,

๐Ÿ”ง Publish Configuration & Migrations for Greater Flexibility

php artisan vendor:publish --provider="TheCoder\World\WorldServiceProvider"

This will publish:
โœ… config/world.php (configuration file)
โœ… Database migration files

๐Ÿ›  Migrate the Database

php artisan migrate

๐ŸŒ Seed Geographical Data

php artisan world:seed

This will populate the database with continents, countries, provinces, and cities.


๐Ÿ” Usage

1๏ธโƒฃ Using the Built-in World Facade (Recommended)

The package now includes a World Facade, making it easier to access data:

use World;

$continents = World::continents()->get();  // Get all continents
$countries = World::countries()->get();  // Get all countries

2๏ธโƒฃ Manually Instantiate the Class

use TheCoder\World;

$world = new World();

$continents = $world->continents()->get();  
$countries = $world->countries()->get();  

Get Countries by Continent

$asia = World::continents('Asia')->first();

$asia = World::continents()->englishNameEqual('Asia')->first();  

$asiaCountries = World::continents()->englishNameEqual('Asia')->countries()->get();  

Get Provinces & Cities

$iranProvinces = World::countries('Iran')->provinces()->get();

$iranProvinces = World::countries()->englishNameEqual('Iran')->provinces()->get();

$gilanCities = World::provinces('Gilan')->cities()->get();  
  
$gilanCities = World::provinces()->englishNameEqual('Gilan')->cities()->get();  

โšกCaching for Maximum Performance

Since the World database is static and never changes, this package supports permanent caching to eliminate redundant database queries and significantly improve performance.

Enable Caching

You can enable caching in the config/world.php file:

  'cache' => [
      'enabled' => true,
      'prefix' => 'thecoder-world-',
      'tag' => 'thecoder-world',
      'ttl' => null, // Store cache forever
  ],

Setting ttl to null ensures that data is cached forever.

Use Cached Data in Queries

If caching is enabled, queries will automatically store results in the cache forever.

Manually Clear Cache (If Needed)

To clear the cache manually, run:

  php artisan cache:clear

Or in code:

World::clearCache();

๐Ÿ— Package-Integrated Laravel Facade

The package now provides a World Facade out-of-the-box, meaning Laravel users don't need to set it up manually.

โœ” No need to register aliases
โœ” Works automatically in Laravel

Just install the package and start using it:

use World;

$continents = World::continents()->get();

๐ŸงชTesting

Ensure you have the necessary dependencies installed:

composer install --dev

โš ๏ธ Database Configuration for Testing

Due to special columns in the database, SQLite is not supported for testing.

Instead, configure MySQL in phpunit.xml:

    <php>
        <env name="DB_CONNECTION" value="mysql"/>
        <env name="DB_HOST" value="127.0.0.1"/>
        <env name="DB_PORT" value="3306"/>
        <env name="DB_DATABASE" value="world_test_database"/>
        <env name="DB_USERNAME" value="root"/>
        <env name="DB_PASSWORD" value=""/>
    </php>

โ–ถ๏ธ Running tests

vendor/bin/phpunit

๐Ÿ—„ Database Schema

This package provides a locations table designed to store structured geographic data with hierarchical relationships.

Column Type Description
id Integer (PK) Unique identifier
continent_id Integer (FK) Parent continent (nullable)
country_id Integer (FK) Parent country (nullable)
province_id Integer (FK) Parent province (nullable)
iso_code String (3) ISO country code (e.g., "US")
type Enum continent, country, province, city
english_name String Location name in English
native_name String Local name (optional)
timezone String Time zone (e.g., "Asia/Tehran")
is_capital Boolean Marks capital cities (default: false)
priority Integer Sort priority (default: 0)
center POINT Geographic coordinates
area MULTIPOLYGON Spatial data for regions

โœ… Indexed for fast queries
โœ… Supports geographic coordinates & boundaries


โ“ FAQ

๐Ÿ”น What is this package used for?

It helps developers retrieve geographical data (continents, countries, provinces, and cities) for Laravel & PHP applications.

๐Ÿ”น Is this package compatible with Laravel?

Yes! It fully supports Laravel and can also be used in vanilla PHP projects.

๐Ÿ”น How can I filter countries by continent?

World::continents('Europe')->countries()->get();

๐Ÿ”น Can I get cities of a specific province?

World::provinces('California')->cities()->get();

โญ Contribute & Support

๐Ÿ”น GitHub Repository: thecoder/world
๐Ÿ”น Issues & Features: Submit Here
๐Ÿ”น Contribute: Fork, star โญ, and submit PRs

๐Ÿ’ก Need more features? Open an issue or contribute to the project!

About

World - PHP & Laravel Package for Geographical Data

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%