i have 2 tables namely users and employers . I would like to match them together like tinder style, which means that when both the user and the employer liked each other , they will each receive a notification .
However i am not sure how would the relationship table will look like??
Right now what i have is in the Relationship Table ,
1.user_id
2.target_employer_id
3.employer_id
4.target_user_id
If i am not wrong , this is considered as a Many - Many relationship.
So , is this table correct???
As I understand your question, in your application, a user can like an employer, and an employer can like a user.
If this is right, it seems clear to me that there are two distinct (many to many) relationships (user => employer, employer => user), and that in your proposed table only two fields are filled at a time.
The best way to represent those relationships is to use two tables.
User to employer fields:
user_id
target_employer_id
Employer to user fields:
employer_id
target_user_id
Depending if you are using Laravel 4 or Laravel 5, you can use one of those two packages to generate the two pivot tables via a migration:
https://github.com/JeffreyWay/Laravel-4-Generators
https://github.com/laracasts/Laravel-5-Generators-Extended
Related
I have a many-to-many relationship between an incident model and a Patient model. An incident can have many patients and a patient can be involved in many incidents.
Should it occur that a user creates duplicates of a patient model we want to be able to merge those two patient models into one. This means that I want to move the incidents that patient 1 is involved in to patient 2 including additional attributes that are sitting on the pivot table.
I've tried something as simple as
Casualty::where('patient_id', $patientOne->getKey())->update(['patient_id' => $patientTwo->getKey()]);
But this doesn't work. Using the updateOnExistingPivot() method would mean I need to iterate over every incident for patient 1 and run a separate DB query to update the patient to patient 2.
I've also tried updating the record like this
$patientOne->incidents()->update(['patient_id' => $patientTwo->getKey()]);
This also doesn't work because there is no patient_id column on the incidents table.
How can I achieve this or am I doing something wrong?
Not sure if I understood you, you want to group more patients into the same accident? You could go with the belongsToMany relation and make one pivot table. Then, when you want to update the data simply use the sync method.
You can also try storing them with json_encode() in one column which will hold only users ID's and later on retrieve them.
Sorry, can't give more info since the question is not described that well.
Say I have four tables:
users
groups
activities
group_activities
Where a group can have any number of activities and an activity can belong to any number of groups through their intermediate table group_activities, and a user belongs to one group via users.group_id. I want to correctly model a relationship between users and activities so that a user can have any one activity, but only if the group that user belongs to has a relation to that activity.
HasOneThrough doesn't seem to work here, since the group the user is related through has multiple activities. HasManyThrough doesn't work since the user can only have one.
I want to properly model this relationship so that it can be picked up for selection automatically via a Nova relationship field, but I'm struggling to figure out exactly how I would do so. My first thought is a HasOneThrough relation with some set of subqueries, but I can't quite piece together where to start.
How would I do this, or conversely, is it possible via Eloquent's automatic relationship system at all?
To ensure we are on the same idea of your relationships:
The relationship between Groups and Activities is a Many To Many relationship (Many To Many - Larvel Documentation).
The group_activities table is the pivot table.
The relationship between users and groups is a One To Many relationship One To Many - Larvel Documentation and the inverse of it One To Many (Inverse) - Larvel Documentation.
To actually answer your question:
If you want to use a shortcut from users to their activities, the Has Many Through is the correct way. If a group can have arbitrary many activities, and a user belongs to one group, the user will be associated to these arbitrary many activities through the group -hence Has Many Through. Note that this is not really a separate relationship though, it's just a convient shortcut.
If you wan't to associate a user with a single Activity directly, you need to to this via a separate One to Many relationship between Users and Activities.
I'm not entirely sure if I interpret your question correctly, so the following is just an assumption, but do you want to ensure a user can only be associated to an activity thats also associated with the user group? So to restrict possible activites by group? If that is the case, you'd simply need to check if the selected activity is in the activities associated with the users group:
With your relationships set up like this:
class User {
public function activity(){
return $this->belongsTo('App\Activity');
}
public function possibleActivities(){
return $this->hasManyThrough('App\Activity','App\Group');
}
}
you can check and associate activities like this:
if( $user->possibleActivities()->contains( $activity ) ){
$user->activity()->associate($activity);
}
I started transforming whole database to a normalised database. But there is one thing which I dont understand about relationships:
Let's say I have 2 tables:
Users
-userID (INT-PK)
-userName (varchar)
_favColor (int)
And:
Colors
-colorID (INT-PK)
-colorName (varchar)
Now obviously I have to create a relationship, the question is:
Should I make relationship between Colors Table and Users Table, or between Users Table and Colors Table?
What I've noticed is that when creating a relationship, the relationship does not appear in both tables, it appears in just one of them, and this makes me confused.
For this example I would recommend a 1:M relationship, going from colours to users.
This is because the users table requires information from the colours table, the reason why you'd have a 1:M relationship is because different users may have the same favourite colour.
I want to design a DB which will be connected to PHP Application. In the app there are two types of users: company and person. Some functionality like adding articles will be done by both so in other tables there are author_id columns. So firstly I decided to create user column.
That's easy: id, username, password, role, active, created where role defines whether user is person or company.
Now I want to add profile table or profile tables depends on what you'd suggest (joined with the previous table by adding profile_id column there).
Both roles have different fields, which are required during registration.
The easiest thing would be to create one table with all required fields for both roles, allow them NULL values and in the PHP app (made in Yii Framework in this case) define requirements for each role in models.
The nicest thing would be to create separate tables for both roles BUT the questions is how to connect these two tables to one table using Foreign Key? Is it even possible. I know I may omit foreign key creation then based on role choose table, and from that table choose profile_id.
Or maybe you have another solution to my problem.
Thanks in advance for replies.
Adrian
You need an intermediary between the page and the database to assign the user to a group that has specific privileges. It's usually accomplished with a user-group-role design.
You can have a table for users system info (username , pass ...), and another for users profile (firstname , birthday ...), and another for groups(superuser , ...).
where user table can have multiple groups: user:one->group:many
user can have one profile user:one->profile:one
I think this is a decent solution.
I am trying to model a database for my current project and I came across a new problem. I have a Project which is supervised by Supervisor, Coordinator and Company. So Project table has Supervisor.id as foreign key and so on. There is also Student table which contains Project.is as a foreign key (because many users can do a project). This is how it is right now. What I would like to do is to have a User table containing a column named type which allows me to see what the role of that particular user is (also student). Even though the table will contain many NULL entries, I will have far less redundant code.
However, the main reason I want to have one User table is that I am using CakePHP and it is not easy to have different models log in. Is this possible in a nice way?
Thanks
EDIT: Maybe I should say that every one of these roles will have different permissions.
I see three tables: USER, GROUP, and ROLE.
Users would be assigned to groups, and groups given roles. I think you need three, not one.
And cardinality matters: I can see where a USER could be assigned to many GROUPS; a GROUP could have many USERS; a ROLE could be assigned to several GROUPS; and a GROUP could have many ROLES. There are many to many JOIN tables as well:
USER <-> USER_GROUP <-> GROUP <-> GROUP_ROLE <-> ROLE
This is normalized - nothing is repeated this way. USER, GROUP, and ROLE have primary keys. The JOIN table primary key is a composite of the two IDs in each row.
It depend on how you will use your associations.
Why not
For example: if you use relation to output data later and you sure, that you database scheme will not changed, than ... why not? your main targets: quality of code and speed of development, so, not matter how much columns with null you will have.
But
if you not sure in your requirements or plan to extend database scheme you can use two columns
supervisor_model
supervisor_id
which will store apropriate values: Company, 77 (I mean that 77 it's id of come Company )
Another approach
You can UUID for you supervisor_id so, it will be unique among several tables and you have not many NULL and extra columns.