【问题标题】:How to get the column from another table in laravel如何从laravel中的另一个表中获取列
【发布时间】:2019-05-09 18:45:37
【问题描述】:

如果用户选择任何课程,我有一个学生创建表单,那么课程费用将显示在下一个输入中。现在课程费用也在另一个名为 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);
    }

//阿贾克斯

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

        });
    }

【问题讨论】:

  • 你有什么错误吗?
  • 鉴于上面的代码示例,这应该按照您希望的方式运行。如果您在 Javascript 中输入 debugger,您会看到 AJAX 调用返回什么?
  • 好的,我已经更新了我的答案。我正在展示我的 ajax 和路由,所以我可能有 ajax 或其他问题
  • 我没有收到任何错误,但它什么也不做
  • student_classes 表中的主键和其他列是什么?

标签: laravel


【解决方案1】:

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

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

【讨论】:

  • 试试这个查询,让我知道响应
  • 即使在后端也没有给我任何回应。我认为我对 ajax 或路由有问题
  • 所以你有一节课和一节课的费用,两者都存储在同一张表中,对吧?如果是,那么我已经用正确的查询更新了我的答案。如果没有,请告诉我更多详情
  • 我在后端也没有得到任何东西
  • 在控制器的开头添加一个 dd 并从检查员那里检查你得到了什么
猜你喜欢
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多