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

jfurnas's avatar

Best way to connect two models

Hello guys. I am still learning laravel, and OOP for that matter, but am catching on quickly. I am working on a system that has two controllers. Organizations, and Employees. There is an interim table called employees_organizations that links employees to individual (or multiple) organizations.

One of the routes in the system is /organizations/{organization-id}/employees, which lists all of the employees linked to this organization.

What is the best way to handle linking these two together, so that I can effectively display how I want it to? I was originally going to use Eloquent Relationships, but since there are three tables, one being the lookup table, I am not sure how I can go about doing this.

The tables are setup like:

organizations- id, name, location

employees- id, name

employees_organizations- id, employee_id, organization_id

The reason for this, instead of just putting the organization id on the individual employee, is that an employee can have multiple organizations they are associated with. The lookup table was the best method for that at the time of design.

Any help would be greatly appreciated.

0 likes
9 replies
jfurnas's avatar

Thank you for that. I was using that page to build relationships for other models, and completely missed the section about the pivot attribute. I believe that is what I would need, correct?

Basically using the employees_organizations as the pivot for displaying the results? That's extremely helpful, and I thank you for your quick response.

jfurnas's avatar

Thanks for the article! I was able to get most of my code completed today using that and the Laravel documentation.

Another question I had, is it possible to extend it one level deeper, and look up an intermediate table based on three models? Employees can be assigned to multiple organizations, and each employee can be in a different department in each organization, so there's potential that you would need to lookup the employees department for a specific organization.

The intermediate table itself would look like this:

department_employee_organization- employee_id, department_id, organization_id

but I am not sure how to have laravel make that relationship, as it appears that in the documentation it's looking for model1_model2 tables when using pivots.

tisuchi's avatar

@jfurnas

Well, you can think of it in different way. For instance-

A employee is belongs to a department and a department is belongs to an organization.

In vise versa, a department has many employee and an organization has many department.

Now your database table should be like this-

employees
- id
- organization_id
- department_id
- some other column
- more column
department
- id
- organization_id
- some other column
- more column
organization
- id
- some other column
- more column

Now you can apply One to Many relationships here.

After doing relationships successfully, you suppose to access data like this way-

//fetch a employee's department and organization 
$employee = Employee::with('department', 'organization')->where('id', 1)->first();

//fetch all employee's and organization of a deparment
$department = Department::with('employee', 'organization')->get();


//fetch all the department and employees of an organization 
$organization = Organization::with('department', 'department.employee')->get();


//here department.employee means that you are accessing department's relationships from organization, that's why . notation for Nested relationship.

Ref: https://laravel.com/docs/5.5/eloquent-relationships#eager-loading

1 like
jfurnas's avatar

@tisuchi doing it that way doesnt work, because it breaks the one employee to many oranizations and one employee to many deparments principal. In your example, I would need to have multiple entries for each employee, one for each department/organization combo.

There needs to be a table that connects employee to organization, which is connected to department.

In a real-world example, let's look at it like this:

Employee Name: John Doe Employee organizations: Organization 1, Organization 2

Departments: Organization 1 - Human Resources, Organization 2 - Catering

tisuchi's avatar

I see.

For you than, Many To Many is the best choose via intermediate table.

Applying pivot table, isn't that solve your issue?

1 like
jfurnas's avatar

Pivot table only seems to work with two models. In this case I have three. I suppose I could write a custom method like this

Public function department( $employee, $organization) { return Db::('department_employee_organization')->Where('employee_id','=','$employee->Id)->where('organization_id','=',$organization->id); }

jfurnas's avatar

Hmm, I was finally able to get it. I ended up connecting them together using a function similar to the one above. It's probably not the conventional or correct way to do it, but it works so I'm going to run with it!

Thanks for your help!

Please or to participate in this conversation.