Select country with Select2

0.0.2 2020-12-15 15:11 UTC

This package is auto-updated.

Last update: 2022-01-15 17:31:57 UTC


Laravel package to select countries single or multiple using select2.


composer require akhaled/select-country

Then add Akhaled\SelectCountry\ServiceProvider::class to config/app.php providers

How to use

1. Add the component to your view

{{-- Single selection --}}
    <x-select-country name="selected_country"></x-select-country>

{{-- Multiple selection --}}
    <x-select-country name="countries[]" multiple="multiple"></x-select-country>

2. Add scripts

{{-- link-to-jquery --}
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/js/select2.min.js"></script>

3. Add select2 style

<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" />


You can use select2 event by listening to element class .akhaled-select-country


    <x-select-country name="selected_country"></x-select-country>
    <div id="country-code"></div>

    $(document).delegate('.akhaled-select-country', 'select2:select', function(e) {
        let country = $(e.params.data.element).data('country')

The option is linked with data-country attribute that's contain more information about the country. See more