Edit: I did some more research I think I got closer but yet it doesn't work. I changed some of the codes.
Hello It's my first question. I searched a lot but I couldn't find my answer. Ok I've got two simple dropdowns which'll be filled with data from a certain database table. the table is called 'mobiles' and the columns are like this :
| id | brand | model | price
it's a simple onepage website. so when the home page controller looks like this:
public function homePage()
{
$brands = Mobile::all('brand')->lists('brand');
return view('pages.home')->with('brands', $brands);
}
I've just populated the mobiles table with 2 phones right now. an Apple iPhone 6 and a Samsung Galaxy S6. And the form looks like this :
<div class="form-group">
{!! Form::label('brand','Brand') !!}
{!! Form::select('brand', $brands, null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('model','Model') !!}
{!! Form::select('model', $models, null, ['class' => 'form-control']) !!}
</div>
First 'the not so important problem' is that I can't find a way to select distinct brand values from my table but that's not the problem right now. the real problem is I don't know the where the problem of my Ajax script is. The Edited Script now is :
<script>
$("document").ready(function(){
$('#brand').change(function(){
$.getJSON("{{ url('api/getmobiles')}}",
{ option: $('#brand option:selected').text() },
function(data) {
var model = $('#model');
model.empty();
$.each(data, function(i, value) {
$('#mobile').append($('<option>').data.model.attr('value', data.id));
});
});
});
});
</script>
and the new route is :
Route::get('api/getmobiles', function() {
$input = Input::get('option');
$models = DB::table('mobiles')->where('brand',$input)->lists('model','id');
return Response::json($models);
});
Please be gentle. I'm new to Laravel. I just found everything on the internet but there seems to be lack of decent tutorial on using Ajax in Laravel.