C
C
chelkaz2019-01-12 20:49:33
Laravel
chelkaz, 2019-01-12 20:49:33

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

2 answer(s)
J
jazzus, 2019-01-12
@jazzus

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');
    }

In the Role model
public function users() {
        return $this->belongsToMany('App\User', 'user_roles', 'role_id', 'user_id');
      }

It could be easier to write, but it's better this way. Now you have a relationship between roles and users through the intermediate table 'user_roles'. Why this is so:
1) You will have one table instead of 10 for any user-related changes. For example, add a new field, change a field, etc. You will do this 1 time, not 10, excluding human error
2) You will want to add access rules. And for this, one more Perm table will be needed. Which will be tied to the roles. And it will be easy and simple to do this in this structure and you will then change access rights in a few clicks.
3) With several user tables, you will have confusing bloated logic with many models, variables, you will torture Laravel, get confused yourself and take everything down.
Okay, the models have been created)) Now, when registering, we will add a role to the user. Let's say a teacher registers and chooses the role of a teacher (let there be a checkbox with the role field where value is the role id).
In the registration controller (if the standard Laravel one) go to the method and write after creating the user . After that, you will automatically create an entry in the intermediate table and the user will be assigned the role of teacher. Ready. Now you can do whatever you want. For example get the users of the specified role in the controller
Correctly wrote? only the numbers need to be changed to constants and in general it’s better to write one method in the model right away to get users of any roles (substitute the name of the role constant through a variable) - but that’s okay, then just transfer it. That's all) Only after creating the roles, you can start working with groups, and this should already be taken out in a separate question.

V
vism, 2019-01-13
@vism

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 question

Ask a Question

731 491 924 answers to any question