name this laravel5.4 eloquent relationship in three tables - php

I studied eloquent relationships in laravel, i love to use them but today am confuse about relation the following three tables has.
I have a relation between three tables like:
1) companies {id, company_name}
2) screens {id, screen_name}
3) company_screen {id, company_id, screen_id, connected[yes/no] }
what type of relation is this in laravel?
how can i fetch screens which are connected(yes), for a company?

after posting my question i read many to many relation in https://laravel.com/docs/5.4/eloquent-relationships#updating-many-to-many-relationships
it's just many to many relation with and extra column in pivot table.
thanks

Related

can't get a specific data in a many-to-many polymorphic relationship in laravel-8

I have a many-to-many polymorphic relationship between the students and the groups, and I also have a one-to-many relationship between the teacher and the groups, so how can I bring all the students from all groups that are related to the teacher, I mean what query I can use to fetch these data?

Laravel belongsToMany Relationship

Having a belongsToMany relation on the same model (Team Model) through custom pivot table (related_teams)
and my relation is like the following
now attaching and detaching is working just fine , however as you see every team belongs to a city
.
so my question is how can i get a list of distinct cities from those attached teams
It depends what you want to achive but for example
$this->attachedHotels->groupBy('city_id');
https://laravel.com/docs/6.x/collections#method-groupby
or
$this->attachedHotels->pluck('city_id')->unique();
https://laravel.com/docs/6.x/collections#method-pluck
https://laravel.com/docs/6.x/collections#method-unique

I need help on Laravel Polymorphic Relationship on Many to Many

I'm current developing a professional site for my organization whereby i have the following models:
employee
id
employee_no
full_name
skill
id
name
sector
id
name
field
id
name
sector_id
The relationship are as following:-
Employee has many Skills and also one or many employees can have one skill
Employee can work in different sectors and also one or many employees can work in one sector
Employee can work in different sector fields and also one or many employees can work in one sector field
as you can see the relationship there is many to many between
employee & skill, employee & sector and employee & fields
so i created three tables to handle these relationships the tables are as following:
employe_skill
id
employee_id
skill_id
employe_sector
id
employee_id
sector_id
employee_field
id
employee_id
filed_id
From the above you may see three tables but almost have the similarity. I need help on how to convert this relationship to polymorphic where i can have only one table.
Despite the similarity between skill and sector the model you propose with three joining tables is completely valid. If the entities differs enough in an conceptual level, different models are completely justified.
Would recommend to create an Entity Relationship diagram to pinpoint the consistency of the system.
Here is how to implement those many to many relationships easily in Laravel.
Good luck!

hasMany vs belongsToMany in laravel 5.x

I'm curious why the Eloquent relationship for hasMany has a different signature than for belongsToMany. Specifically the custom join table name-- for a system where a given Comment belongs to many Roles, and a given Role would have many Comments, I want to store the relationship in a table called my_custom_join_table and have the keys set up as comment_key and role_key.
return $this->belongsToMany('App\Role', 'my_custom_join_table', 'comment_key', 'role_key'); // works
But on the inverse, I can't define that custom table (at least the docs don't mention it):
return $this->hasMany('App\Comment', 'comment_key', 'role_key');
If I have a Role object that hasMany Comments, but I use a non-standard table name to store that relationship, why can I use this non-standard table going one way but not the other?
hasMany is used in a One To Many relationship while belongsToMany refers to a Many To Many relationship. They are both distinct relationship types and each require a different database structure - thus they take different parameters.
The key difference is that in a One To Many relationship, you only need the two database tables that correspond to the related models. This is because the reference to the relation is stored on the owned model's table itself. For instance, you might have a Country model and a City model. A Country has many cities. However, each City only exists in one country. Therefore, you would store that country on the City model itself (as country_id or something like that).
However, a Many To Many relationship requires a third database table, called a pivot table. The pivot table stores references to both the models and you can declare it as a second parameter in the relationship declaration. For example, imagine you have your City model and you also have a Car model. You want a relationship to show the types of cars people drive in each city. Well, in one city people will drive many different types of car. However, if you look at one car type you will also know that it can be driven in many different cities. Therefore it would be impossible to store a city_id or a car_id on either model because each would have more than one. Therefore, you put those references in the pivot table.
As a rule of thumb, if you use a belongsToMany relationship, it can only be paired with another belongsToMany relationship and means that you have a third pivot table. If you use a hasMany relationship, it can only be paired with a belongsTo relationship and no extra database tables are required.
In your example, you just need to make the inverse relation into a belongsToMany and add your custom table again, along with the foreign and local keys (reversing the order from the other model).
Try to understand with text and a figure.
One to One(hasOne) relationship:
A user has(can have) one profile. So, a profile belongs to one user.
One to many(hasMany):
A user has many(can have many) articles. So, many articles belong to one user.
Many to many(BelongsToMany):
A User can belong to many forums. So, a forum belongs to many users.

Tinder Laravel matching between 2 models

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

Categories