Full package with brazilian cities.
Run in your terminal:, (*2)
composer require "ruwer/brcities":"dev-master"
Open the file config/app.php and add to "providers":, (*3)
'providers' => [ // ... Ruwer\BRcities\BrcitiesServiceProvider::class, ],
Let's publish the package files into your project. Run in your terminal:, (*4)
php artisan vendor:publish
Now you have the following files in your project: - database/migrations/2014_10_12_000000_create_cities_table.php - database/seeds/CitiesTableSeeder.php, (*5)
You also have a copy of brcities.js in the following folders of your project: - public/js/brcities.js - resources/assets/js/brcities.js, (*6)
So, run in your terminal:, (*7)
composer dump-autoload php artisan migrate php artisan db:seed --class="CitiesTableSeeder"
Done! :), (*8)
Now you can use your new model City, e.g.:, (*9)
use Ruwer\BRcities\City; $states = City::select("state")->distinct("state")->orderBy("state")->get(); $pr_cities = City::where("state", "=", "PR")->orderBy("name")->get();
You also have the following routes returning JSON, e.g.: - myproject.dev/states - myproject.dev/cities/PR, (*10)
Create your form:, (*11)
<form id="form"> <select name="state" class="js-state"></select> <select name="city" class="js-city"></select> </form>
Include the .js file:, (*12)
<script src="{{ asset('js/brcities.js') }}"></script>
Call the script:, (*13)
<script> var cities = new Cities("#form"); </script>
Define the class:, (*14)
//My Personal Options var options = { stateInput: ".js-state", //default cityInput: ".js-city", //default short: false //default (Long state names) } //Call the Script var cities = new Cities("#form", options);
Set placeholders:, (*15)
<select name="state" class="js-state"> <option value="">Please select...</option> </select> <select name="city" class="js-city"> <option value="">Please select the state first...</option> </select>
Default value:, (*16)
<select name="state" class="js-state" value="PR"></select> <select name="city" class="js-city" value="3595"></select>