ruwer/brcities

Full package with brazilian cities.

dev-master 2018-11-29 17:14 UTC

This package is not auto-updated.

Last update: 2024-11-13 19:45:47 UTC


README

Codacy Badge Total Downloads Build Status

  • A Laravel 5 package containing all Brazilian cities to your database.
  • Auto-fill the cities when you select the state. Built using Vanilla JS.
  • Works well with Cep Package.

Installation

Run in your terminal:

composer require "ruwer/brcities":"dev-master"

Open the file config/app.php and add to "providers":

'providers' => [
    // ...
    Ruwer\BRcities\BrcitiesServiceProvider::class,
],

Let's publish the package files into your project. Run in your terminal:

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

You also have a copy of brcities.js in the following folders of your project:

  • public/js/brcities.js
  • resources/assets/js/brcities.js

So, run in your terminal:

composer dump-autoload
php artisan migrate
php artisan db:seed --class="CitiesTableSeeder"

Done! :)

Model

Now you can use your new model City, e.g.:

use Ruwer\BRcities\City;

$states = City::select("state")->distinct("state")->orderBy("state")->get();
$pr_cities = City::where("state", "=", "PR")->orderBy("name")->get();

Routes

You also have the following routes returning JSON, e.g.:

  • myproject.dev/states
  • myproject.dev/cities/PR

Usage

Create your form:

<form id="form">
  <select name="state" class="js-state"></select>
  <select name="city" class="js-city"></select>
</form>

Include the .js file:

<script src="{{ asset('js/brcities.js') }}"></script>

Call the script:

<script>
  var cities = new Cities("#form");
</script>

Options

Define the class:

//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:

<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:

<select name="state" class="js-state" value="PR"></select>
<select name="city" class="js-city" value="3595"></select>