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

时间:2019-03-26 16:18:35

标签: php laravel dropdown

你好,我是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();
            //
            // }

        });

    });

这是我尝试过的

1 个答案:

答案 0 :(得分: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)
   }
})