Skip to content

Instantly share code, notes, and snippets.

@placecodex
Forked from sabid/Database
Created February 15, 2021 22:16
Show Gist options
  • Select an option

  • Save placecodex/9b80a01667a65e242bf758b4492a70f5 to your computer and use it in GitHub Desktop.

Select an option

Save placecodex/9b80a01667a65e242bf758b4492a70f5 to your computer and use it in GitHub Desktop.

Revisions

  1. @sabid sabid revised this gist Jul 24, 2014. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions Database
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    CREATE TABLE `paises` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `nombre` varchar(50) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

    CREATE TABLE `ciudades` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `pais_id` int(11) DEFAULT NULL,
    `nombre` varchar(50) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
  2. @sabid sabid created this gist Jul 24, 2014.
    70 changes: 70 additions & 0 deletions Laravel + Jquery Ajax Country and City.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    Script
    ------------------------------
    <script type="text/javascript">
    $('select#country').change(function(){
    var countryId = $(this).val();
    $ciudaditems = $('.cityItems').remove();
    $.get('/ciudades/'+countryId, function(data){
    $.each(data, function(index, element){
    //console.log(element);
    $('select#city').append('<option value="'+element.id+'" class="cityItems">'+element.nombre+'</option>')
    });
    }, 'json');
    });
    });
    </script>

    View
    ------------------------------
    Country :
    <select name="country" class="country" id="country">
    <option selected="selected">--Seleccionar Pais--</option>
    @foreach($paises as $pais)
    <option value="{{ $pais->id }}">{{ $pais->nombre }}</option>
    @endforeach
    </select> <br/><br/>

    Ciudad :
    <select name="city" class="city" id="city">
    <option selected="selected">--Seleccionar Ciudad--</option>
    </select>

    Route
    --------------------------
    Route::get('/ciudades/{id}', function($id)
    {
    $pais_id = $id;

    $ciudades = Pais::find($pais_id)->ciudades;


    return Response::json($ciudades);
    });

    Model Pais
    ------------------------


    class Pais extends \Eloquent {
    protected $table = 'paises';

    public function ciudades()
    {
    return $this->hasMany('ciudad');
    }

    }

    Model Ciudad
    -----------------------


    class Ciudad extends \Eloquent {
    protected $table = 'ciudades';

    public function pais()
    {
    return $this->belongsTo('pais');
    }

    }