通过下拉列表从数据库中获取数据Laravel

问题描述 投票:0回答:1

您好我是laravel的新手我通过下拉列表从数据库获取数据时遇到问题。我有一个类的下拉列表,所以当用户选择任何一个类并单击提交按钮时,那个与该类相关的学生将显示在下面的表中...我在数据库中有一个表,用于存在学生类ID的学生为每个学生。我无法真正理解这个问题的逻辑。有人可以帮帮我吗?

//我的控制器

 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);

    }

//我的看法

        <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>

// 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) {




                        $('table[id="studentsData"]').empty();

                        $.each(data, function(key, value) {

                            $('table[id="studentsData"]').append('<td   value="'+ key +'">'+ value +'</td  >');

                        });


                    }

                });

            }

            // else{
            //
            //     $('table[id="studentsData"]').empty();
            //
            // }

        });

    });

这是我试过的

php laravel dropdown
1个回答
0
投票

我认为你的查询错了。

public function mytableAjax($id)
{
    $students = Student::where('class_id', $id)->get();
    // if you try dd($students), you should see it in Network > Preview

    return response()->json(['students => $students]);
}

然后,

$.ajax({
   url: '/myform/ajax/'+classID,
   type: "GET",
   success:function(data) {
      console.log(data.students)
   }
})
© www.soinside.com 2019 - 2024. All rights reserved.