ricardoruwer / BRCities by ricardoruwer

Full package with brazilian cities.
4,862
12
1
Package Data
Maintainer Username: ricardoruwer
Maintainer Contact: ricardoruwer@gmail.com (Ricardo Ruwer)
Package Create Date: 2016-05-04
Package Last Update: 2018-11-29
Language: PHP
License: MIT
Last Refreshed: 2024-12-14 15:00:45
Package Statistics
Total Downloads: 4,862
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 12
Total Watchers: 1
Total Forks: 3
Total Open Issues: 0

BiRCities for Laravel 5

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>