Created
October 12, 2017 07:57
-
-
Save ahmadshobirin/03aff541ddd226f8dbd7f73a4f3ef590 to your computer and use it in GitHub Desktop.
select ajax dynamically from database laravel
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
//my enviroment | |
// laravel 5.5 | |
//in web.php | |
Route::get('admin/get-merk-product/{id}','ProductController@getMerk'); | |
// in controller | |
use Response; | |
use App\MMerk; | |
class ProductController extends Controller | |
{ | |
public function getMerk($typeId) | |
{ | |
$dataMerk = MMerk::where('jenis_id','=',$typeId)->orderBy('name','ASC')->get(); | |
return Response::json($dataMerk); | |
} | |
} | |
//in blade for javascript | |
<script type="text/javascript"> | |
var langId = "{{asset('vendor/select2/js/i18n/id.js')}}"; | |
$(document).ready(function () { | |
//on change type | |
$('#type').change(function (e) { | |
$.ajax({ | |
url: "<?= url('/admin/get-merk-product/') ?>/" + $(this).val(), | |
method: 'GET', | |
success: function (data) { | |
//console.log(data); | |
$('#merk').children('option:not(:first)').remove().end(); | |
$.each(data,function(index,merkObj){ | |
$('#merk').append('<option value="'+merkObj.id+'"> '+merkObj.name+' </option>') | |
}); | |
} | |
}); | |
}); | |
}); | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment