【发布时间】: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