There seems to be an error "Cannot use 'in' operator to search for 'length'" while trying to display data in a table. The URL successfully executes the GET action, but the error occurs when attempting to display the data in the table. Here is the JavaScript code:
<script type="text/javascript>
$(document).ready(function(){
$('#kategori').on('change', function(e){
var id = e.target.value;
$.get('/khs/khs_semester'+id, function(data){
console.log(id);
console.log(data);
$('#khs').empty();
$.each(data, function(index, element){
$('#khs').append("<tr><td>"+element.kode_mk+"</td><td>"+element.nama_mk+"</td>"+
"<td>"+element.semester+"</td></tr>");
});
});
});
});
</script>
public function merkAjax($id){
if($id==0){
$terpilih = \DB::table('khs')
->join('matakuliah','matakuliah.kode_mk','=','khs.kode_mk')
->join('dosen','dosen.nidn','=','khs.nidn')
->where('khs.nim',$nim)
->where('khs.semester','1')
->get();
}else{
$terpilih = \DB::table('khs')
->join('matakuliah','matakuliah.kode_mk','=','khs.kode_mk')
->join('dosen','dosen.nidn','=','khs.nidn')
->where('khs.nim',$nim)
->where('khs.semester',$id)
->get();
}
return $terpilih;
}