Answer the question
In order to leave comments, you need to log in
How to make connections between four and one initially non-existent object?
Hello, it's hard for me to understand.
Example: There are three tables, the first is student classes, it stores classes, for example:
id name
1 1A
2 7D
3 3B
The second is students, for example:
id name
1 Petya
2 Denis
3 Oleg
Another table of teachers:
id name
1 Valentina
2 Elena
3 Angela
In the public part there is a drop-down list of classes, and a simple list of students.
For example, Valentina logged in
Task - she wants to choose a class, for example 7D and mark students in order to create a certain group or add to an existing one. And then here's what's important!
You need to create a group that will have a class and selected students.
If, for example, class 7D is selected and we are trying to add students, then we check, if there is no such group, then we create it, but if it exists, we add it to it. After that, as at least one group is created, a new drop-down list appears, from the created group and the button - "Create group" by clicking on which, a new group with an active class is created.
What is the best way to make connections from these tables? And what is the best way to think about the storage structure?
Answer the question
In order to leave comments, you need to log in
No matter how much you would like to create 3 tables for the same entities now, don't do it. Advice from a beginner) Then you are tormented by refactoring, and refactoring the database structure means demolishing everything and doing it again. And you will do it. Because users in different tables will bring you problems.
It is necessary to make models (tables)
User (all users)
Role (name of roles - teachers, students, etc.)
UserRole (with user_id and role_id fields)
User is associated with Role through ManyToMany
In the user model
public function roles()
{
return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');
}
public function users() {
return $this->belongsToMany('App\User', 'user_roles', 'role_id', 'user_id');
}
Just create a
school_groups table:
name
class_id
type_id(100% types will appear)
school_group_users
school_group_id
user_id
user_object(if the user can be a teacher or student)
admin_id
admin_object(if the admin can be a teacher or student)
or instead of admin_id and admin_object make the access_type field and put there like access is simple and everything
Of course, you can do it through the system of roles and accesses, but this time it will be 100 times more labor-intensive and more expensive.
Well, if you ask such a question, you won’t do it normally, so don’t get into the role system.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question