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

pranaycb's avatar

Suggest me best laravel schema

I am developing an educational institute management system where I need to assign teacher, subject and paper to the courses. So I've created a schema course_subject_paper_teacher where I am storing the course, subject, paper and teacher's id. But things are getting so much complicated when I am going to fetch data from that table. For example I need to show all subjects of a course in comma (,) separated format. Also the subject name along with its papers and contents separately and unique teachers (one teacher can teach many subject and paper in a course) record along with their teaching subjects in comma separated format. Can you please suggest a better database schema for my current requirement? Thanks in advanced 😊

0 likes
7 replies
martinbean's avatar

@pranaycb You should be separating those things if they’re distinct.

  • If a course can have multiple subjects, then you need a course_subject pivot table.
  • If a teacher can teach many subjects, then you need a subject_teacher pivot table.

And so on. You should not be storing anything comma-separated in a single column; if you are then this is a sign it should be a pivot table instead.

1 like
pranaycb's avatar

@martinbean No I mean I need to show the name in comma separate format. Basically I am assigning a teacher for a particular subject and a particular paper. For example in course id of 1, teacher id of 1 can teacher the paper of id 1 of the subject of id 1

martinbean's avatar

No I mean I need to show the name in comma separate format

@pranaycb What does that have to do with your database schema? Fetch the names, then join them with a comma:

echo implode(', ', $namesArray); // i.e. John Doe, Jane Doe, Joe Bloggs
pranaycb's avatar

@martinbean I need to store the record of which teacher is teaching which subject and which paper of a particular course. For that I'd used course_subject_paper_teacher where there are 4 foreign keys - course_id, subject_id, paper_id and teacher_id. This becomes complicated when fetching data from that table. Since teacher assignment are subject and paper specific (teachers are not bound in a specific subject or paper, means they can teach multiple subject based on the assignment).

pranaycb's avatar

@martinbean Basically in the course details page there are 3 tabs. First tab I need to show all teacher assigned to the course with their teaching subjects (in comma separated format). In the details tab I need to show the subject name, subject paper name and their content (content stores as json in the paper table).

martinbean's avatar

@pranaycb Again, that has no bearing on your database schema. It wouldn’t matter if you wanted to show names in a comma-separated format, a definition list, a table, a QR code… it wouldn’t change how you would model and store that data.

Stop conflating how you’d structure and store that data, with how you’d present that data.

Please or to participate in this conversation.