Hello all .. I am new to laravel
I want to get the students according the selected class in the dropdown... I applied some ajax for this... I have students table where the student_class_id exist and another table of classes but i dont know why i ma getting nothing in my console .. Let me show you guys what i had done for this
//My 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("students_class_id",$id)->lists("class_name","id");
return dd($students);
}
//My view and ajax
Take Attendance
<select name="students_class_id" class="form-control" style="width:350px">
<option value="">--- Select State ---</option>
@foreach ($classes as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
@endforeach
</select>
<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>
@stop
@section('script')
<script>
$(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) {
console.log(data);
}
});
}
});
});
</script>
//My routes
Route::get('students/attendance/index',array('as'=>'myform','uses'=>'AttendanceController@mytableAjax'));
Route::get('myform/ajax/{id}',array('as'=>'myform.ajax','uses'=>'AttendanceController@index'));
Help me pls