Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lewanay's avatar

Getting data thourgh ajax

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

0 likes
6 replies
Snapey's avatar

your routes appear to be transposed

Route::get('students/attendance/index',array('as'=>'myform','uses'=>'AttendanceController@mytableAjax')); Route::get('myform/ajax/{id}',array('as'=>'myform.ajax','uses'=>'AttendanceController@index'));

instead

Route::get('students/attendance/index','AttendanceController@index'); Route::get('myform/ajax/{id}','AttendanceController@mytableAjax);
lewanay's avatar

Yes sir . Thats working now im getting some data like this in the console but i think this is not desired data i want

message Method Illuminate\Database\Query\Builder::lists does not exist. exception BadMethodCallException file C:\xampp\htdocs\sms\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php line 2816 trace […] 0 {…} file C:\xampp\htdocs\sms\app\Http\Controllers\AttendanceController.php line 27 function __call class Illuminate\Database\Query\Builder type -> 1 {…} function mytableAjax class App\Http\Controllers\AttendanceController type -> 2 {…} file C:\xampp\htdocs\sms\vendor\laravel\framework\src\Illuminate\Routing\Controller.php line 54 function call_user_func_array 3 {…} 4 {…} 5 {…} 6 {…} 7 {…} 8 {…} 9 {…} 10 {…} 11 {…} 12 {…} 13 {…} 14 {…} 15 {…} 16 {…} 17 {…} 18 {…} 19 {…} 20 {…} 21 {…} 22 {…} 23 {…} 24 {…} 25 {…} 26 {…} 27 {…}

lewanay's avatar

Also getting this message in the playload

"message": "Method Illuminate\Database\Query\Builder::lists does not exist.",

lewanay's avatar

Ok sir I changed my controller code from this $students = DB::table("students")->where("students_class_id",$id)->lists("class_name","id"); to this $students = Student::where('students_class_id', $id)->get(); And its working pretty well now

lewanay's avatar

@SNAPEY - Sir now i am getting the correct data but the problem is my view I only get the text something like this in my table instead of the data [object Object] Whats the problem ?

Snapey's avatar
Snapey
Best Answer
Level 122

you are following some old tutorial. lists has been deprecated for about 3 years. Replace with pluck

Please or to participate in this conversation.