Как достать колонку из другого стола в laravel - PullRequest
0 голосов
/ 09 мая 2019

У меня есть форма создания для студентов, где я хочу, чтобы, если пользователь выбрал какой-либо класс, плата за обучение будет показана в следующем входе. Теперь плата за обучение также определена в другой таблице с именем 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');

        });
    }

стол студентов_класса

 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 Ответ

3 голосов
/ 09 мая 2019

Я думаю, что вы возитесь с именами столбцов. После просмотра вашего запроса, если я пойду с именами столбцов, попробуйте следующее:

public function getStudentFee($id) {
    $studentFee = DB::table("students_classes")->where("id", $id)->pluck("class_fee","id");
    return response()->json($studentFee);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...