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

AbdulBazith's avatar

how to link all 5 tables with user table. using polymorphic table or any other idea

i have finished a school project.but due to this vacation iam restructuring the project.

i need to get the students data in the school application.

students data i differentiated into

Academic info
personal info
parents info
siblings info
address info
previous education info

so i need to ling all the tables with student_id

in my old project what i did is

i had a form to get all the above details in a single form and then what i did is

// first few data will be stored in user table

$user = User::create([
            'year_id' => $request->acc_year_id,
            'user_name' => $username,
            'user_type' => 'student',
            'admission_number' => $request->admin_no,
            'user_phno' => $request->stud_sms_no,
            'password' => $request->admin_no,
            'status' => $request->status,
        ]);

so for each student an account will be created this is because student also has logins thats why. next

 $last_id = $user->id;

 $admininfo = new StudentAdmissionInfo;

        $admininfo->student_id = $last_id;
        $admininfo->admin_no = trim($request->admin_no);
        $admininfo->admin_date = trim($request->admin_date);
        $admininfo->admin_class_id = trim($request->admin_class_id);
        $admininfo->admin_section_id = trim($request->admin_section_id);
        $admininfo->stud_roll_no = trim($request->stud_roll_no);
        $admininfo->stud_type = trim($request->stud_type);
        $admininfo->status = trim($request->status);
        $admininfo->save();
.
.
.


the remaining data will be stored in corresponding table with student_id which is created above.

so by this user table will be parent table and all other will be the child table.

everything went correct.

now iam restructuring the work. let the format be correct else can i use polymorphic table??

if i use polymorphic table how i can link all the tables to a single student. and how i can add the student in user table??

kindly some one suggest.

i have another doubt

iam storing the student address in this format

StudentAddressInfo

id  student_id  addr_type   address pincode state   country
1   1           temporary   1-1      555       TN   india
2   1           permanent   2-2      666       TN   india
3   2           both        5-5      777       TN   india

IF a student have two type of address like temporary and permanent this format ok.

most of the student have same address for that how i can store this in studentAddressInfo??

their permanent and present address will be same. how i can store. what i did is

just changes the addr_type and both but while editing i face lots of issue

Kindly some one help please

0 likes
4 replies
AbdulBazith's avatar

@tarang19 thank you for your response.

actually it is possible by polymorphic table i know.

the matter is see

StaffTable  ------ UserTable -----Student table   this is possible as polymorphic table.

because staff may have different columns and student may have different columns  both of them will involve in  usertable. so everything is ok.

But my doubt is studenttable is further splited into 5 tables.

if my guessing is right is this ok


Stafftable----- userTable----StudentTable (this acts as parent table) -> address info table, parentsinfo table . and soo on ( child tables)

is this right?? just i need to keep a parent table and its child table?? for student info. student info itself has 6 tables spliited. so the to be merged with user table??

How???

tarang19's avatar

as i understand you need to store table id in parent table and add relationship using laravel relationship

for example use this url for guide may be help you

Links: https://www.codechief.org/article/polymorphic-relationship-in-laravel https://blog.logrocket.com/polymorphic-relationships-in-laravel/ https://appdividend.com/2018/05/18/laravel-polymorphic-relationship-tutorial-example/

           https://stackoverflow.com/questions/55784729/belongstomany-with-4-tables-in-laravel

Please or to participate in this conversation.