Problem with select and use of ajax in Laravel 5

Asked

Viewed 409 times

0

I am trying to implement a select with ajax in Laravel 5 have the following situation.

a state model class

class Estados extends Model {

protected $table = 'estados';


public function cidades() {
    return $this->hasMany( 'App\Models\Painel\Cidades', 'id_estado', 'id');
 }
}

a city model class

class Cidades extends Model {
protected $table = 'cidades';

public function pessoaFisica() {
    return $this->hasMany('App\Models\Painel\PessoaFisica');
}

public function estado() {
    return $this->belongsTo ( 'App\Models\Painel\Estados', 'id_estados' );
}
}

then did the following in my view did the following two selects and a js method.

<div class="form-group">
                <label>Estado
                    <select name="estado" id="estado" class="form-control input-sm">
                        <option value="">Escolha o Estado</option>
                        @foreach($estados as $estado)
                            <option value="{{$estado->id}}">{{ $estado->uf }}</option>
                        @endforeach
                       </select>
                </label>
            </div>



            </div>

            <div class="col-sm-4">

                    <div class="form-group">
                    <label>Cidade
                    <select id="cidade" class="form-control input-sm" name="cidade">
                    <option value=""></option>
                    </select>
                    </label>
                    </div>

            </div>

follow the js

 <script>
     $('#estado').on('change', function(e){
         console.log(e);
         var estado = e.target.value;

         $.get('/estados?estado' + estado, function(data) {

             $('#cidade').empty();
             $.each(data, function(index,subcatObj){
                 $('#cidade').append('<option value="'+subcatObj.id+'">'+ subcatObj.nome </option>);
             });
         });
     });
 </script>

to populate my status select in my controller I did the following.

public function getAdicionar() {

    $estados = Estados::all();

    return view("painel.admin.adicionarpessoafisica", compact("estados"));

}

and last but not least I did the following on my route.

Route::get('/estados',function() {
    $estado = Input::get('estado');

    $cidades = Cidades::where('id_estado', '=', $estado)->get();

    return Response::json($cidades);

});

The problem that is happening is the following when trying to load the second select that depends on the first nothing is happening.

  • What’s the problem? It’s in Javascript, you can’t understand.

  • it is true I forgot to say what is happening, is not loading the second select... I believe yes it is the no js

  • edited the question @Guilhermenascimento

  • What does the browser console show? There is some syntax error or loading failure in the Network tab?

  • There’s no way out

  • You answered your state var to see if it’s not empty?

  • The answer solved the problem?

Show 2 more comments

1 answer

1

You did it:

   $.get('/estados?estado' + estado, function(data) {

But what’s right is this:

   $.get('/estados?estado=' + estado, function(data) {

A sign of =

Browser other questions tagged

You are not signed in. Login or sign up in order to post.