Git Product home page Git Product logo

laravel-world's Introduction

Laravel World Database

This package focused on World Countries, Regions, and Cities database with locale support for Laravel.

Conceptions

There are 5 main objects in this package.

  • World: the earth world.
  • Continent: 7 continent
  • Country: 248 countries
  • Division: Divisions such as state/province.
  • City: the last level of region, some cities up to Country, some up to Division.

Attributes

Common attributes:

  • name: Common name of region(english).
  • full_name: Full name or official name(english).
  • code: ISO-3166-1-alpha2/ISO-3166-2 code
  • local_name: translation of Common name
  • local_full_name: translation of full name
  • local_alias: alias in different language
  • local_abbr: Abbreviation

Country spec attributes:

  • emoji: Emoji flag of country
  • capital: Captial of this country
  • code_alpha3: Code of ISO-3166-1-alpha3
  • currency_code: ISO-4177 Currency Code, e.g. USD, CNY
  • currency_name: ISO-4177 Currency Name,
  • local_currency_name: ISO-4177 Currency name in locale

Example:

use Khsing\World\World;
$china = World::getByCode('cn');
$china->setLocale('zh-cn');
$china->name; // China
$china->local_name; // **
$china->full_name; // People's Republic of China
$china->local_full_name; // 中华人民共和国
$china->emoji; // 🇨🇳
$china->callingcode; // 86
$china->code; // CN
$china->code_alpha3; // CHN
$china->has_division; // true
$china->currency_code; // CNY
$china->currency_name; // Yuan Renminbi
$china->local_currency_name; // 人民币

Localization

Right now, only English(default and fallback) and Chinese-Simp zh-cn are supported. Locale settings is following Laravel project settings in config/app.php.

Setup

  • composer require
composer require khsing/world
  • Add Service Provider into config/app.php, (Only required before Laravel 5.5)
'providers' => [
    // ...
    Khsing\World\WorldServiceProvider::class,
]
  • Publish and init
php artisan vendor:publish --force --provider="Khsing\World\WorldServiceProvider"
composer dump-autoload
php artisan world:init

Usage

  • get all Continent
use Khsing\World\World;

World::Continents()
  • get all Countries
use Khsing\World\World;

World::Countries()
  • get country/city/division by code
use Khsing\World\World;

World::getByCode('cn'); // iso-3166 alpha 2 code
World::getByCode('chn'); // iso-3166 alpha 3 code
World::getByCode('cn-11'); // Beijing
  • get countries belong to a continent
use Khsing\World\Models\Continent;

$asia = Continent::getByCode('AS');
$countries = $asia->countries()->get();
// or use children method
$countries = $asia->children();
  • get continent or parent
$china = Country::getByCode('cn');
$asia = $china->parent();
  • get division/state/province via Conutry
$china = Country::getByCode('cn');
$provinces = $china->divisions()->get()
// or use children method
$provinces = $china->children();
  • get cities via Country or Division.
$china = Country::getByCode('cn');
// check has_division to determine next level is division or city.
$china->has_division; // true, otherwise is false
$regsions = $china->children();

Contributions

If you want contribute to this library, issue and pr are welcome. please following those steps.

  1. start a new laravel project and install this library.
  2. install orangehill/iseed.
  3. modify datas via sql.
  4. generate seeds via artisan iseed world_cities,world_cities_locale,world_continents,world_continents_locale,world_countries,world_countries_locale,world_divisions,world_divisions_locale
  5. replace delete() with truncate(), cd database/seeders/ && sed -i 's/->delete()/->truncate()/g' World*.php
  6. copy seeds files into library.
  7. commit your work. ;)

TODO

  • change the way to seed data, eg. loading data from json?
  • add front-end support
  • find a way to update dataset

Data Sources

Thanks

About

This package published under MIT license. If you have any question or suggestion, please feel free to submit a issue, or email me Guixing<khsing.cn(AT)gmail.com>.

Have a nice day.

laravel-world's People

Contributors

abhishek6262 avatar amirolzolkifli avatar balduinofernando avatar chinleung avatar dependabot[bot] avatar geoffreyvanwyk avatar human018 avatar kauto avatar khsing avatar mednasserallah avatar mokhosh avatar nauman-coeus avatar nbyloff avatar rbridge avatar sitehandy avatar uovidiu avatar yswtrue avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-world's Issues

Disable "seeding" multiple locales

So today I figured out your library is so complete, however I don't like it seeding data in multiple locales as the only thing I need is English.

Is it possible to prevent installing CN or any other locales than English (per an option in the init command or so?

Call to undefined function Khsing\World\starts_with()

I have installed laravel-worl and initiated. Whenever I call any method , for eg: World::getByCode('cn'); getting this error Symfony\Component\Debug\Exception\FatalThrowableError
Call to undefined function Khsing\World\starts_with()
. Please help asap.

Illuminate\Database\QueryException ( Specified key was too long )

php artisan world:init
Execute migrate first, migrating...
Migrating: 2017_04_08_163453_create_world_cities_locale_table
Migrated: 2017_04_08_163453_create_world_cities_locale_table (80.40ms)
Migrating: 2017_04_08_163453_create_world_cities_table

Illuminate\Database\QueryException

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table world_cities add index uniq _city(country_id, division_id, name))

at vendor\laravel\framework\src\Illuminate\Database\Connection.php:678
674▕ // If an exception occurs when attempting to run a query, we'll format the error
675▕ // message to include the bindings with SQL, which will make this exception a
676▕ // lot more helpful to the developer instead of just the database's errors.
677▕ catch (Exception $e) {
➜ 678▕ throw new QueryException(
679▕ $query, $this->prepareBindings($bindings), $e
680▕ );
681▕ }
682▕

  1. vendor\laravel\framework\src\Illuminate\Database\Connection.php:471
    PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes")

  2. vendor\laravel\framework\src\Illuminate\Database\Connection.php:471
    PDOStatement::execute()

Data seems to be incorrect or needs updating

There are a many countries in the world_countries table where the value for has_divisions is set to 0, but there is data for these countries in world_divisions table.

Is that intentional or the does the data needs to be updated for this column?

Anyone else come across this?

Adding new division and city via code

Hi, I want to add divisions and cities via code.

We just need to add this code to the model for example adding it just below the protected $table = 'world_divisions'; code

protected $guarded = [];

Thanks!

!!!!BUG!!!!!!

World::getByCode('abd') not working with cities codes!
I really need it. Can you fix it soon?

Laravel 9

I love your work!
Any chance of upgrading to support Laravel 9 soon?

Adding more cities

UK only has 63 cities listed.

What is the source of the data? Can the seeds be reproduced?

Not Supported with laravel 5.7

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

publishing is not working for laravel 5.7

Getting all locations

I am trying to set up an autocomplete with this repo. Although that would mean that I need to get all locations irrespective of continent, country or city, so that I can create this giant library for location search.

But I am having difficulty in doing this because I don’t know how to get all locations associated with the repo. Can I get help on this please?

Illuminate\Database\QueryException

SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into migrations (migration, batch) values (2017_04_08_163453_create_world_cities_locale_
table, 4))

at\vendor\laravel\framework\src\Illuminate\Database\Connection.php:678
674▕ // If an exception occurs when attempting to run a query, we'll format the error
675▕ // message to include the bindings with SQL, which will make this exception a
676▕ // lot more helpful to the developer instead of just the database's errors.
677▕ catch (Exception $e) {
➜ 678▕ throw new QueryException(
679▕ $query, $this->prepareBindings($bindings), $e
680▕ );
681▕ }
682▕

1 \vendor\laravel\framework\src\Illuminate\Database\Connection.php:471
PDOException::("SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value")

2 \vendor\laravel\framework\src\Illuminate\Database\Connection.php:471
PDOStatement::execute()

any help with this one?
this is happenin' when I run php artisan world:init

Timezones?

How can we add timezone to each city?

Problem migrating when using sqlite

Hello,

Trying to use sqlite as database in testing caused error when it tries to migrate.
Error was: General error: 1 index uniq_locale already exists

The problem might be using same index name even for different tables.

Reference

Including World Languages

@khsing
Are you accepting contributions to this repo?

If so I'd love to add world languages to it. Does that fit within your expectations for this package?

Invalid Date format

While i am migrate DB, getting this error.
Illuminate\Database\QueryException : SQLSTATE[22007]: Invalid datetime format

Cannot make migration

Hello, after write in composer "php artisan world:init" cmd shows me this:

"PDOException::("SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xF0\x9F\x87\xA8\xF0\x9F...' for column 'emoji' at row 1")"

How to fix it?

Problems with with seeders

I couldn't get seeders to run properly because:

  1. they were published to Seeds folder instead of Seeders
  2. they didn't have nay namespace on them so they couldn't be found
  3. some classes had problems with imports

not shown all cities,

$country = World::getByCode("ph");
$regsions = $country->children();

return $regsions;

and it only shows 4 cities.

Publish Migrations

Hi first +1 to this package. However there is some need that the worlds... migration runs before the other migration tables. For example I have users table that has country_id, city_id references world_countries ... Can this be added to the vendor publish? so that we can modify the naming? as I see the publish command only seeds.

PS- I am running artisan migrate:fresh --seed or artisan db:migrate for the first time. Let me know if this is something you would like I can raise a PR

Mongodb support

I tried using this package with my mongodb project but got stuck in the seeding phase.
This is the result i got from running php artisan world:init
migrations error

Recreate:
create a new project, add Jenssegers Laravel Mongodb to the project and configure it as the default database for the project. Install khsing Laravel World package and follow the installation steps.

Previous Debug Steps:
I tried changing all migrations and seed to implement the specified mongodb alternatives in the root folder of khsing package in vendor but still unable to fix it but get this error:

Symfony\Component\Debug\Exception\FatalThrowableError
Call to a member function prepare() on null

SQLSTATE[42000]: Syntax error or access violation: 1071

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table world_cities add index uniq_city(country_id, division_id, name))

This error occurred when php artisan world:init command is executed

767 bytes is the stated prefix limitation for InnoDB tables in MySQL version 5.6

Some of the multi column indices in
2017_04_08_163453_create_world_countries_table.php
2017_04_08_163453_create_world_divisions_table.php
Exceeded the 767 byte limit of MySQL version 5.6

I modified the tables so instead of names being 255 chars, they were 170 chars.
Not sure if this will have side effects later on, but thought I would post here just incase it helps someone else.

Schema::create('world_countries', function(Blueprint $table)
{
	$table->increments('id')->comment('Auto increase ID');
	$table->integer('continent_id')->unsigned()->comment('Continent ID');
	$table->string('name', 170)->default('')->comment('Country Common Name');
	$table->string('full_name', 255)->nullable()->comment('Country Fullname');
	$table->string('capital', 255)->nullable()->comment('Capital Common Name');
	$table->string('code', 4)->nullable()->comment('ISO3166-1-Alpha-2');
	$table->string('code_alpha3', 6)->nullable()->comment('ISO3166-1-Alpha-3');
	$table->string('emoji', 16)->nullable()->comment('Country Emoji');
	$table->boolean('has_division')->default(0)->comment('Has Division');
	$table->string('currency_code', 3)->nullable()->comment('iso_4217_code');
	$table->string('currency_name', 128)->nullable()->comment('iso_4217_name');
	$table->string('tld', 8)->nullable()->comment('Top level domain');
	$table->string('callingcode', 8)->nullable()->comment('Calling prefix');
	$table->unique(['continent_id','name'], 'uniq_country');
});
Schema::create('world_divisions', function(Blueprint $table)
{
	$table->increments('id')->comment('Auto Increase ID');
	$table->integer('country_id')->unsigned()->comment('Country ID');
	$table->string('name', 175)->default('')->comment('Division Common Name');
	$table->string('full_name', 255)->nullable()->comment('Division Full Name');
	$table->string('code', 64)->nullable()->comment('ISO 3166-2 Code');
	$table->boolean('has_city')->default(0)->comment('Has city?');
	$table->unique(['country_id','name'], 'uniq_division');
});

Entry missing

Hi,

First of all thank you for such an amazing library. So i found a missing entry in the file. Let me explain the steps i followed.

  • So I didnt have privileges to set SET FOREIGN_KEY_CHECKS=0 and then to 1 which is default in your WorldTableSeeder.php file
  • I removed the foreign key of world_cities and world_cities_locale
  • Upon enabling after running the seeders it got an error Key (city_id) = (49) is not present in table (world_cities)
  • I checked out the seed file WorldCitiesTableSeeder.php and there is an entry missing
  • After id => 49 there is direct id => 50 code between line 444 to 460
  • Where as in file WorldCitiesLocaleTableSeeder.php between line 453 to 460 city_id => 49 is referenced

Can you kindly update the file :)

Versions change

I have changed versions pattern, the version will follow with Laravel version. e.g. v6.x is for Laravel 6.x, the master brunch will always follow heads of Laravel.

Thanks

Invalid country information

I think some countries have wrong continents. For example these Asian countries are listed as European:

  • Armenia
  • Azerbaijan

Some Morocco cities are missing

>>> use Khsing\World\World;
>>> $morocco = World::getByCode('ma')->cities->pluck('name');
=> Illuminate\Support\Collection {#3074
     all: [
       "Casablanca",
       "Fes",
       "Marrakech",
       "Meknes",
       "Oujda",
       "Rabat",
       "Tangier",
       "Tetouan",
       "Western Sahara",
     ],
   }

Shows only 9 cities, there's about 50 cities.
Any easy way to edit the seeder without breaking any relationship?

The error code for the world_divisions

Hello, thanks for sharing the library. As check from the world_division, there is a small error between the code for these 2 rows of record, which is country_id = 87, name = "Pahang" and "Pulau Pinang", as found the code is not correct, it should be "ph" for "Pahang" and "pg" for "Pulau Pinang", kindly please update it. Thanks :)
As checked from the web, it is recommended to put the code to "png" for the "Pulau Pinang" rather than only "pg", same for others with the country_id = 87.

This is Awesome

Hi there..

I just review this package. and already give a shot to try this. and it feels awesome. Thanks so much for this incredible package.

With this package then I just continue create the api.

anyway, I was wondering how do you know all of the data?

Laravel 10

This is very great work.
Any support for Laravel 10?

Incomplete Data of Countries/Divisions

I was using this package, But it doesn't have a complete record of all countries e.g Pakistan doesn't have any divisions
Please complete the db if you need any assistance from me I'll be happy to assist you :)

@khsing

Table not found on php artisan world:init run

Illuminate\Database\QueryException

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ecast.world_cities_locale' doesn't exist (SQL: alter table world_cities_locale add constraint world_cities_locale_ibfk_1 foreign key (city_id) references world_cities (id) on delete CASCADE on update RESTRICT)

Can not `php artisan world:init'

I have followed the step, but it occurs an error when I did php artisan world:init.
I got this error Duplicate table: 7 ERROR: relation unique_locale has already exist
I don't know why and I have checked all my table and I does not have the unique_locale

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.