Last active
August 29, 2015 14:18
-
-
Save MisterMike/9b3ce860a5f1645294c9 to your computer and use it in GitHub Desktop.
AJAX second SELECT population (Laravel Dynamic Dropdown)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Routes.php : | |
/* AJAX Select DropDown Population */ | |
Route::get('api/dropdown', function(){ | |
$input = Input::get('catselect'); | |
$cat = Category::find($input); | |
$subcategory = $cat->subcategory(); | |
return Response::make($subcategory->get(['id','subcat_name'])); | |
}); | |
// Category.php (Model) | |
class Category extends Eloquent { | |
public function Subcategory(){ | |
return $this->hasMany('Subcategory'); | |
} | |
public function Seed(){ | |
return $this->belongsTo('Seed', 'seed_category'); | |
} | |
} | |
// Subcategory.php (Model) | |
class Subcategory extends Eloquent { | |
public function Category(){ | |
return $this->belongsTo('Category'); | |
} | |
public function Seed(){ | |
return $this->belongsTo('Seed', 'seed_subcategory'); | |
} | |
} | |
// Seed.php (Model) | |
class Seed extends Eloquent { | |
public function source() { | |
return $this->belongsTo('Source', 'id'); | |
} | |
public function category() { | |
/* seconad argument is row in target table*/ | |
return $this->hasOne('Category', 'id'); | |
} | |
public function subcategory() { | |
return $this->hasOne('Subcategory', 'id'); | |
} | |
} | |
//new.blade.php (View) | |
<div class="row"> | |
<div class="col-md-6"> | |
<div class="form-group"> | |
<span class="formlabel">Kategorie *</span> | |
<label class="select"> | |
<select class="form-control" id="seed_category" name="seed_category" data-url="{{ url('api/dropdown')}}"> | |
<option value="0" selected disabled="">Bitte aus Liste wählen</option> | |
@foreach ($catlist as $category) | |
<option value="{{$category->id}}" >{{$category->cat_name}}</option> | |
@endforeach | |
</select> | |
<i></i> | |
</label> | |
</div> | |
</div> | |
<div class="col-md-6"> | |
<div class="form-group"> | |
<span class="formlabel">Unterkategorie *</span> | |
<label class="select"> | |
<select class="form-control" id="seed_subcategory" name="seed_subcategory"> | |
<option value="0" selected disabled="">Bitte zuerst eine Kategorie wählen</option> | |
</select> | |
<i></i> | |
</label> | |
</div> | |
</div> | |
</div> | |
<script type="text/javascript"> | |
$(function() { | |
// Autopopulate Subcategories Select Drop down | |
$('#seed_category').change(function(){ | |
$.get($(this).data('url'), | |
{ catselect: $(this).val() }, | |
function(data) { | |
var subcat = $('#seed_subcategory'); | |
subcat.empty(); | |
$.each(data, function(index, element) { | |
subcat.append("<option value='"+ element.id +"'>" + element.subcat_name + "</option>"); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment