Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

Error in Student Mark DB structure insert and view

Guys iam working with a project School Management System

I have a small problem in entering marks of the student for specific class.

i have a form to add the student mark. first need to choose the class , section which subject(Eg: English), and which Exam (Eg: Quarterly exam). for that whole class english mark for quarterly exam is entered. now the problem is in db. how to frame the structure.

i have the following tables

SubjectDetails with columns (which has Subject details. just shown few columns only)

id      subjectcode     subjectName
1       SB01            English
2       SB02            Maths
.
.
.

ExamDetails with columns (which has Exam details. just shown few columns only)

id      Examcode        examName
1       Ex01            Quarterly Exam
2       Ex02            Halfyearly Exam

User with columns (which has student details. just shown few columns only)

id  user_name   class   section
1   Abdul       V       A
2   Bazith      V       A
3   Imran       V       A
4   Muthu       V       A

Now i need to mingle the above three tables and to have a new table to enter the student marks. how to do that, thats my question

what i did is had two tables

MainMark with columns

id  class_id    section_id  exam_id
1   V       A           1

SubMark with columns

id  mainmark_id     student_id  subject_id  Mark
1       1           1           1           80
2       1           2           1           90
3       1           3           1           75
4       1           4           1           60      

In the above table student_id and mark is changing. here for the same exam same class, same section i have entered the marks. so i given (exam, class, section) in mainmark table and for different student for different subject their mark changes so i given that in Submark table.

But fetching is the big issue for me. i think i done wrong some where. i cant predict it. kindly someone suggest your suggestion please.

The output i expect is when i choose a class and section and the exam, the student name with subject and marks must be displayed. this what i expect. Kindly some one help

0 likes
5 replies
AbdulBazith's avatar

@snapey

I think after long gap iam tagging you. Kindly suggest your idea please.

just need to enter student mark.

the tables already have are ClassTable, UserTable,ExamTable,SubjectTable these are the tables i have. how to mingle this and to have a mark table. please suggest me an idea please.

AbdulBazith's avatar

My big Problem is intermeidate table. i cant get how to use intermediate table and whatever columns present in that table. Guys Sorry for tagging You all please.

@snapey @bobbybouwmann @tykus @martinbean @Nakov @jlrdw

You all guys answered me a lot of threads before. thats why iam tagging you. kindly suugest you opinion please,

i need to enter student mark How many tables i need to use.

i have the following tables AccademicYearTable, ClassTable, UserTable(studentdetails), SubjectTable, ExaminationTable Based on these tables i need to enter student mark.

Kindly suggest

AbdulBazith's avatar

I will explain what i have tried,

My tables are just two for the mark

MainMark with columns

id      class_id        section_id      exam_id
1       1           1               1
2       1           1               2

SubMark with columns

id  mainmark_id     student_id  subject_id  mark
1   1               1           1           80
2   1               2           1           90
3   1               3           1           50
4   1               4           1           60

in the above table for a single subject that is (subject: english) i have entered mark for 4 students, the exam name, class section are refered from the mainmark table. how to fetch this as my expected output below

class: V STD        Section: A      Exam: Quarterly Exam

StudentName     English     Maths       Science
Bazith              80          40          60
Abdul               60          70          80
Imran               69          75          85

Like above i need the output,

what i tried in coding is my controller

 $students = StudentMainMark::where(function ($query) use ($request) {

            if (!empty($request->class_id)) {

                $query->where('class_id', $request->class_id);
            }
            if (!empty($request->section_id)) {

                $query->where('section_id', $request->section_id);
            }
            if (!empty($request->exam_id)) {

                $query->where('exam_id', $request->exam_id);
            }

        })->with('studentsubmark')->get();

In the above code iam fetching records based from mainmark table with class and section and exam id. with its sub mark.

when i dd($students);

Collection {#546 ▼
  #items: array:1 [▼
    0 => StudentMainMark {#545 ▼
      #connection: "mysql"
      #table: "student_main_marks"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [▼
        "id" => 34
        "acc_year_id" => 1
        "exam_date" => "2019-12-12"
        "class_id" => 6
        "section_id" => 16
        "exam_id" => 2
        "exam_remarks" => ""
        "exam_status" => "active"
        "created_at" => "2019-12-12 07:26:04"
        "updated_at" => "2019-12-12 07:26:04"
      ]
      #original: array:10 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "studentsubmark" => Collection {#576 ▼
          #items: array:4 [▼
            0 => StudentSubMark {#580 ▼
              #fillable: array:7 [▶]
              #connection: "mysql"
              #table: "student_sub_marks"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:10 [▼
                "id" => 35
                "acc_year_id" => 1
                "main_mark_id" => 34
                "student_id" => 10
                "subject_id" => 12
                "mark" => 57.0
                "student_remarks" => null
                "status" => "active"
                "created_at" => "2019-12-12 07:26:04"
                "updated_at" => "2019-12-12 07:26:04"
              ]
              #original: array:10 [▶]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #guarded: array:1 [▶]
            }
            1 => StudentSubMark {#581 ▶}
            2 => StudentSubMark {#582 ▶}
            3 => StudentSubMark {#583 ▶}
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    }
  ]
}

in my view. blade file

<table class="table">
    <thead>
        <tr class="unread bggrn">
            <th>S.No</th>
            <th>Name</th>

            @foreach($students->studentsubmark as $sub)
            <th>{{  $sub->subject->sub_name }}</th>
            @endforeach

        </tr>
    </thead>
    <tbody class="capital">
        @foreach($students->studentsubmark as $sub)
        <tr class="profile-table">
            <td> {{ $loop->iteration }} </td>
            <td> <a href="">{{ $sub->user->user_name }} </a> </td>
            <td> {{  $sub->mark }} </td>
            <td>{{ $sub->student_remarks }}</td>
        </tr>
        @endforeach

    </tbody>
</table>

Expected Output:

StudentName     English     Maths       Science
Bazith              80          40          60
Abdul               60          70          80
Imran               69          75          85

in the above view iam trying to make a output like mentioned. but the subjects and name are repeated. why?

and this are my models

Main Mark Model
class StudentMainMark extends Model
{
    
    public function studentsubmark()
    {
        return $this->hasmany('App\StudentSubMark', 'main_mark_id');
    }
}



SubMark Model

public function studentmainmark()
    {
        return $this->belongsTo('App\StudentMainMark', 'main_mark_id');
    }

public function user()
    {
        return $this->belongsTo('App\User', 'student_id');
    }
    
    public function subject()
    {
        return $this->belongsTo('App\SubjectInfo', 'subject_id');
    }


But i face a problem is the names and subjects are repeated in the view. blade as for loop is mistake or something else i dont know

martinbean's avatar

@abdulbazith Sorry, but I'm not on-call. I'll answer your question if I see it and have time to, not because I'm tagged.

Please or to participate in this conversation.