Hasnain Kahn
Hasnain Kahn

Reputation: 119

How to get the column from another table in laravel

I have a create form for students where I want if a user select any class then the class fee will be shown in the next input. Now the class fee is also defined in another table named as students_classes. I am using ajax for it but the only problem is the query builder. I dont how I can get the fee which i had defined in the students_classes table using query builder. I am using something like this but its not working

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>\

Routes

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

students table

 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 table

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

        });
    }

Upvotes: 1

Views: 378

Answers (1)

Sahil
Sahil

Reputation: 462

I think you are messing with the column names. After looking your query, if i go with the column names then try this one:

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

Upvotes: 2

Related Questions