@lewanay I do also have the same requirement. Is any chance can you help me?
Retrieving data from database via drop down through ajax | Laravel
Hello everyone.... I am new to laravel I want to get the data from database to my table with dropdown through ajax. I have dropdown for classes so when the user select any class then the students related to that class will be displayed.. I write some code in ajax for this but cannot get the full logic... Below is my code
// Ajax
$(document).ready(function() {
$('select[name="students_class_id"]').on('change', function() {
var classID = $(this).val();
if(classID) {
$.ajax({
url: '/myform/ajax/'+classID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[id="studentsData"]').empty();
$.each(data, function(key, value) {
$('select[id="studentsData"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}else{
$('select[name="studentsData"]').empty();
}
});
});
</script>
//Controller public function index() { $classes = StudentsClass::pluck('class_name', 'id')->all(); $students = Student::all(); return view('admin.students.attendance.index', compact('classes', 'students')); }
public function mytableAjax($id)
{
$students = DB::table("students")
->where("student_id",$id)
->lists("class_name","id");
return json_encode($students);
}
//My view
<div class="ml-4 mr-4 py-2">
<h3 class="text-center display-4">Take Attendance</h3>
<!-- SELECT -->
<div class="form-group col-md-12 d-flex">
{!! Form::select('students_class_id', [''=>'Choose Class'] + $classes, null, ['class'=>'form-control mr-2', 'id'=>'class_id']) !!}
{!! Form::button(' Show', ['type'=>'submit', 'name'=>'show', 'class'=>'fas fa-folder-open btn btn-warning']) !!}
</div>
<table id="studentsData" class="table table-striped table-bordered table-list-search">
<thead>
<tr>
<th>#</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Attendance</th>
</tr>
</thead>
@foreach($students as $student)
<tbody>
<tr>
<th>{{$student->id}}</th>
<td>{{$student->student_id}}</td>
<td>{{$student->first_name}} {{$student->last_name}}</td>
<td>
<div class="form-group">
<select class="form-control" id="gender">
<option>Present</option>
<option>Absent</option>
<option>Leave</option>
</select>
</div>
</td>
</tr>
</tbody>
@endforeach
</table>
<a class="fas fa-folder-open btn btn-success float-right mb-4 mr-2"> Save</a>
</div>
Its not getting anything but only all students and the classes in the dropdown
Please or to participate in this conversation.