Git Product home page Git Product logo

countries-laravel's Introduction

Countries for Laravel

World Map

Latest Stable Version License Code Quality Build

Coverage PHP Downloads StyleCI

What does it gives you?

This package has all sorts of information about countries:

info items
taxes 32
geometry maps 248
topology maps 248
currencies 256
countries 266
timezones 423
borders 649
flags 1,570
states 4,526
cities 7,376
timezones times 81,153

Validation

The validation is extending Laravel's validation, so you can use it like any other validation rules, like

/**
 * Store a new blog post.
 *
 * @param  Request  $request
 * @return Response
 */
public function store(Request $request)
{
    $this->validate($request, [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
        'country' => 'country' //Checks if valid name.common
    ]);

    // The blog post is valid, store in database...
}

Which validation rules there is and what there name should be, can all be configured in the configuration file.

'validation' => [
    'rules' => [
	    'countryCommon' => 'name.common'
	]
]

By changing the configuration like this, we can now access the property name.common, by the validation rule countryCommon

You have to define all the validations rules in settings, only a few is defined by default, the default is

'rules' 	=> [
    'country' 			=> 'name.common',
    'cca2',
    'cca3',
    'ccn3',
    'cioc',
    'currencies'			=> 'ISO4217',
    'language',
    'language_short'	=> 'ISO639_3',
]

Documentation

This package is a Laravel bridge, please refer to the main package repository for more information and docs.

Requirements

  • PHP 7.0+
  • Laravel 5.5+

Installing

Use Composer to install it:

composer require pragmarx/countries-laravel

Publishing assets

You can publish configuration by doing:

php artisan vendor:publish --provider=PragmaRX\\CountriesLaravel\\Package\\ServiceProvider

Usage

After installing you'll have access to the Countries Façade, and the package is based on Laravel Collections, so you basically have access to all methods in Collections, like

$france = Countries::where('name.common', 'France');

Flag routes

You can refer directly to an SVG flag by linking

/pragmarx/countries/flag/download/<cca3-code>.svg
/pragmarx/countries/flag/file/<cca3-code>.svg

Examples:

https://laravel.com/pragmarx/countries/flag/download/usa.svg
https://laravel.com/pragmarx/countries/flag/file/usa.svg

http://pragmarx.test/pragmarx/countries/flag/file/usa.svg

These routes can be turned off in the configuration file:

'routes' => [
    'enabled' => false,
]

Author

Antonio Carlos Ribeiro

License

Countries is licensed under the MIT License - see the LICENSE file for details

Contributing

Pull requests and issues are more than welcome.

countries-laravel's People

Contributors

andershagbard avatar antonioribeiro avatar lionslair avatar ryantheallmighty 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

countries-laravel's Issues

Latest Updates broken

Just did a composer update and the latest update appears to have broken things.

Illuminate\Foundation\ComposerScripts::postAutoloadDump
@php artisan package:discover

In Container.php line 767:

Class PragmaRX\Countries\Package\Services\Cache does not exist

It's in the PragmaRX\Countries package. I cleared my composer cache reinstalled all and still occurs.

Validators do not get added due to deferred service provider

I just upgraded from 0.5.2 and discovered that the validations no longer work, throwing a BadMethodCallException: Method Illuminate\Validation\Validator::validateCca2 does not exist. I discovered that the validator extensions were not being loaded at all, and traced that back to the service provider now being deferred.

As a workaround, I just added app('pragmarx.countries'); to my own AppServiceProvider to manually resolve the provider on every request, which then registers the validator extensions, and everything is happy again.

I'm not sure of the best solution to this issue. Perhaps it would be best to convert the validation extensions to Rule objects that will resolve the provider when needed without having to disable deferred loading all together.

Country Flags are not shown on windows OS

When i open website from Linux OS flags are shown as they should
1
but when i open it using the windows 10 OS, i see letters
2

Anyone have such issue ? How to fix it ?

*Feature* Localization The Countries

Hello,
Thank You, Mr @antonioribeiro For This Cool Package 📦

It Would Be Cool If The Is A Localization Feature So Any One Could Use Its Own Languages
For Example I May Want To The Countries Name To Be In Arabic
Brazil Become برازيل In Arabic

I Believe This Would Be Helpful Of Course We Are Ready To Contribute

This package shouldn't pull in "Illuminate Agnostic" packages

Since this package is specifically for Laravel, which it depends on, it shouldn't pull in these redundant packages that duplicate Laravel functionality. It may be minor, but each additional file, class, and function loaded in a project can have an impact on performance.

Translate all queries (suggestion)

It is possible to put one of the translations by default.

It is that my system is in Spanish, and I would like to list in a pluck all the countries, but that they are translated at once, and any query that this one makes in Spanish.

I saw that in laravel there is a configuration file but do not notice how to put by default all the results are in spa

Collection whereIn not working

$countries= \Countries::whereIn('cca2', $array)->sortBy('name')->pluck('name.common', 'cca2')->toArray();
Is this not supported anymore?

I was using pragmarx/countries:0.1.1 (way too old version) which get me wrong name for czech republic.
So now trying to update to latest version we needed to pull in this package but sadly whereIn was not working for this package.
Was i doing anything wrong? or there is different methods now?

No cache is being created.

I recently switched from the countries package to the countries-laravel package.

I've cleared the composer cache and ensured that the initial package has been removed. Im working on a 'local' environment using Laravel 5.8.

The config states:

cache' => [
    'enabled' => true,

    'service' => PragmaRX\Countries\Package\Services\Cache\Service::class,

    'duration' => 180,

    'directory' => sys_get_temp_dir().'/__PRAGMARX_COUNTRIES__/cache',
],

The previous package had the same configuration, but the directory was set to sys_get_temp_dir().'/__pragmarx-countries-cache__'.

Now when I run a piece of code which accesses countries, the same __pragmarx-countries-cache__ directory is created in my system tmp directory where it should be creating __PRAGMARX_COUNTRIES__ and still no cache files are being generated.

I've tried setting my environment to production, but this didnt help.

From following the debugger through the PragmaRX\Countries\Package\Services\Cache\Service it looks like the configuration is completely ignored

Cache configuration option is ignored

/config/countries.php

return [

'cache' => [
    'enabled' => false,
    ...

PHP Warning: fopen(/tmp/pragmarx-countries-cache/_%008ba3f0fd390277cf0a5308fd42dccf46): failed to open stream: No such file or directory

Unable to publish configuration file

According to documentation, then it should be possible to publish the packages config file using

php artisan vendor:publish --provider=PragmaRX\Countries\ServiceProvider

But no configuration file is generated.

Clearing the cache with php artisan config:clear does not seem to fix the issue nor does php artisan cache:clear

I am currently testing on Laravel 5.5 (and I am using this repository in composer: "pragmarx/countries-laravel": "^0.5.2")

Can't seem to install on Laravel 5.3?

I think there's an issue with the thightenco/collect dependency because when I want to install the package it says:

    - pragmarx/coollection v0.5.1 requires tightenco/collect >=5.5.33 -> satisfiable by illuminate/support[v5.5.33, v5.5.34, v5.6.0, v5.6.1, v5.6.2, v5.6.3], tightenco/collect[v5.5.33, v5.6.0, v5.6.11, v5.6.12, v5.6.13, v5.6.14, v5.6.15, v5.6.16, v5.6.17, v5.6.18, v5.6.19, v5.6.20, v5.6.4, v5.6.5, v5.6.6, v5.6.7, v5.6.8, v5.6.9].

And then tells me "Conclusion: don't install laravel/framework" which is pretty ironic. :p

Array select methods like whereIn and whereNotIn don't seem to work

If I try to select multiple countries using an array based on the Collection methods whereIn or whereNotIn, I get no results.

I.e., the following return an empty collection:

Countries::whereIn("name.common", ["Netherlands", "Belgium"])->pluck("name.common");
Countries::whereNotIn("name.common", ["Netherlands", "Belgium"])->pluck("name.common");

While in the same context these work fine:

Countries::where("name.common", "Netherlands")->pluck("name.common");
Countries::where("name.common", "Belgium")->pluck("name.common");

Validation rules won't work until manual loading the provider once

Hi @antonioribeiro ,

Thank you for your work on this package.
I have a question on the validation rules that I don't seem to get working. It looks like the validation rules are not yet loaded before you load app('pragmarx.countries'); explicitly. After that the validation does work correctly.

Version "pragmarx/countries-laravel" === '0.6.3'

Example steps to reproduce the error

class PostController extends Controller

    public function store(Request $request)
    {
        // Without this the 'cca2' validation rule gives error. Please toggle on/off.
        $foo = app('pragmarx.countries');

        $validated = $this->validate($request, [
           'country_code' => 'nullable|string|size:2|cca2',
        ];

        dd($validated);
    }

The error I get when the rule is not loaded:

Method Illuminate\Validation\Validator::validateCca2 does not exist.

Perhaps this has something to do with the defered state in \PragmaRX\CountriesLaravel\Package\ServiceProvider ?

Can't install on Laravel 7

There is an error when installing on Laravel 7:

Problem 1
    - Conclusion: remove nesbot/carbon 2.31.0
    - Conclusion: don't install nesbot/carbon 2.31.0
    - pragmarx/ia-collection v5.5.33 requires nesbot/carbon ~1.20 ->

Laravel 5.6 causes " Call to undefined method Monolog\Logger::listen()"

I installed it on Laravel 5.6. It gives "Call to undefined method Monolog\Logger::listen()" error

vendor/laravel/framework/src/Illuminate/Log/LogManager.php

     * @param array  $context
     *
     * @return void
     */
    public function log($level, $message, array $context = [])
    {
        return $this->driver()->log($level, $message, $context);
    }
 
    /**
     * Dynamically call the default driver instance.
     *
     * @param  string  $method
     * @param  array   $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->driver()->$method(...$parameters);
    }
}
 

error on this line
return $this->driver()->$method(...$parameters);

Weird encoding for Brazil city names

Thanks for a great package! It works well for generating a list of Brazillian states.

I'm just struggling with the cities. This doesn't work:

Countries::where('name.common', 'Brazil')
  ->first()
  ->hydrateCities()
  ->cities
  ->where('adm1name', 'Amapá')
  ->pluck('name')
  ->toArray();

But this does:

Countries::where('name.common', 'Brazil')
  ->first()
  ->hydrateCities()
  ->cities
  ->where('adm1name', 'Amapá')
  ->pluck('name')
  ->toArray();

I'm on Laravel 7, PHP 7.4.

What can I change in what I'm doing to get it to filter the cities by the state name?

How to show country flag by abbr?

Hello,
In my Laravel 8 app I need to show country flag and I installed package
antonioribeiro/countries-laravel

But I did not fin if it supports abbreviation(like ‘AF’,'FR') ?

I trierd to make like :
$nextFlag = Countries::where('abbreviation', 'AF');
and several options like 'abbreviation', but in all cases I ghot empty collection...

Which way is valid ?

Thanks!

Laravel v6 Support

Your package is making calls to string helper functions such as camel_case which have been removed in Laravel 6. It would be nice to get an update on this package so I don't have to pull in the string helpers package just to make stuff work again whenever this package is called in the application.

Usage of flags directly

Hi @antonioribeiro.

I have a (probably dumb) question.

How do you use the flags directly?

I've hydrated it and they're coming on the Countries...

The thing is that they just spit out some html and I've used it with the:

{!! $country->flag-icon !!}     // prints <span class="flag-icon flag-icon-abw"></span>

or

{!! $country->flag-icon-squared !!}      // prints <span class="flag-icon flag-icon-abw flag-icon-squared"></span>

or even

{!! $country->world-flags-sprite !!}     // prints <span class="flag abw"></span>

It seems that you are using the convention

<span class="flag-icon flag-icon-{{ strtolower($country->ccn3) }}"></span> // this works

and every package ob github that matches this "style" is using another convention

<span class="flag-icon flag-icon-{{ strtolower($country->cca2) }}"></span>    // this is what I found online on https://github.com/lipis/flag-icon-css

Target class [PragmaRX\CountriesLaravel\Package\Update\Updater] does not exist

I think there's an issue when updating countries because when I want run php artisan countries:update with the package ("pragmarx/countries-laravel": "^0.7.0"), it says:

 Illuminate\Contracts\Container\BindingResolutionException

  Target class [PragmaRX\CountriesLaravel\Package\Update\Updater] does not exist.

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:835
    831▕
    832▕         try {
    833▕             $reflector = new ReflectionClass($concrete);
    834▕         } catch (ReflectionException $e) {
  ➜ 835▕             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
    836▕         }
    837▕
    838▕         // If the type is not instantiable, the developer is attempting to resolve
    839▕         // an abstract type such as an Interface or Abstract Class and there is +22 vendor frames

  23  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

My Laravel version :


$ php artisan -V
Laravel Framework 8.34.0

How to reproduce :
Run countries update artisan command after installing the package on Laravel 8

php artisan countries:update

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.