如何从laravel中的另一个表中获取列

时间:2019-05-09 10:14:17

标签: laravel

我为学生创建了一个表格,如果用户选择任何班级,则班级费用将显示在下一个输入中。现在,班级费用也定义在另一个名为students_classes的表中。我正在使用ajax,但唯一的问题是查询生成器。我不知道如何使用查询生成器来获取我在students_classes表中定义的费用。我正在使用类似的东西,但是它不起作用

public function getStudentFee($id) {
        $studentFee = DB::table("students_classes")->where("class_fee",$id)->pluck("students_class_id","id");
        return json_encode($studentFee);
    }

// Ajax

    jQuery(document).ready(function ()
    {
        jQuery('select[name="class_id"]').on('change',function(){
            var ClassID = jQuery(this).val();
            if(ClassID)
            {
                jQuery.ajax({
                    url : 'students/ajaxID/' +ClassID,
                    type : "GET",
                    dataType : "json",
                    success:function(data)
                    {
                        console.log(data);
                        jQuery('input[name="class_fee"]').empty();
                        jQuery.each(data, function(key,value){
                            $('input[name="class_fee"]').append('<option value="'+ key +'">'+ value +'</option>');
                        });
                    }
                });
            }
            else
            {
                $('input[name="class_fee"]').empty();
            }
        });
    });

</script>\

路线

Route::get('students/ajaxID/{id}',array('as'=>'students.ajaxID', 'uses'=>'StudentsController@getStudentFee'));

学生桌

 public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->increments('id');
            $table->string('student_id')->unique();
            $table->string('first_name');
            $table->string('last_name');
            $table->string('DOB');
            $table->integer('students_class_id');
            $table->string('gender');
            $table->string('blood_group');
            $table->string('religion');
            $table->string('photo_id');
            $table->string('student_address');
            $table->integer('student_phone_no');
            $table->string('guardian_name');
            $table->string('guardian_gender');
            $table->string('guardian_relation');
            $table->string('guardian_occupation');
            $table->integer('guardian_phone_no');
            $table->string('email')->unique();
            $table->string('NIC_no');
            $table->string('guardian_address');
            $table->string('discount_percent');
            $table->string('total_fee');
//            $table->string('document_id');
            $table->string('username');
            $table->string('password');
            $table->timestamps();


//            $table->foreign('students_class_id')->references('id')->on('students_classes')->onDelete('cascade');

        });
    }

students_classes表

 public function up()
    {
        Schema::create('students_classes', function (Blueprint $table) {
            $table->increments('id');
            $table->string('class_name');
            $table->string('class_fee');
            $table->string('class_teacher');
            $table->timestamps();

        });
    }

1 个答案:

答案 0 :(得分:3)

我认为您正在弄乱列名。 查看您的查询后,如果我使用列名,请尝试以下操作:

public function getStudentFee($id) {
    $studentFee = DB::table("students_classes")->where("id", $id)->pluck("class_fee","id");
    return response()->json($studentFee);
}