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
{{-- Single selection --}}
<div>
<x-select-country name="selected_country"></x-select-country>
</div>
{{-- Multiple selection --}}
<div>
<x-select-country name="countries[]" multiple="multiple"></x-select-country>
</div>
{{-- link-to-jquery --}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
@selectCountryScripts
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
You can use select2 event by listening to element class .akhaled-select-country
<div>
<x-select-country name="selected_country"></x-select-country>
<div id="country-code"></div>
</div>
<script>
$(document).delegate('.akhaled-select-country', 'select2:select', function(e) {
let country = $(e.params.data.element).data('country')
$('#country-code').text(`+${country.calling_code}`);
});
</script>
The option is linked with data-country attribute that's contain more information about the country. See more