3

I am trying to implement a feature and I don't think it's available by relationships.

A User may have many Companies and Company may have many Users. They are connected via company_employees pivot table.

pivot table looks like this:

company_id, user_id, position_id

I want to have an option to fetch company employees with their assosiated position.

Let's say there are companies:

  1. "Foo Company"
  2. "Bar Company"

and user

  1. "John Doe"

There are also 2 positions:

  1. "Frontend Developer"
  2. "Backend Developer"

John Doe is an employee in both of these companies with different position.

company_id | user_id | position_id
1            1         1
2            1         2

How can i make something like

$company = Company::first();
$company->employees()->with('position')->get();

return only position associated with first user that belongs to first company as HasOne relationship?

5
  • Exactly which model you want to return? I don't really get what do you want to return Commented Mar 28, 2019 at 9:47
  • why are employees linked by user ids, and not employee_ids for example? Commented Mar 28, 2019 at 10:07
  • @CodeBoyCode There is not such thing as Employee model per se. Employee is simply User associated with Company. Commented Mar 28, 2019 at 10:10
  • @CloudSohJunFu I want to return all users that belongs to the company (that works already through many-to-many) and ONLY the POSITION associated with that specific company. Commented Mar 28, 2019 at 10:10
  • Make a relations to user and position in pivot_table model and one relation to pivot_table from Company model and then try Company::with(['pivot_table.user','pivot_table.position'])->first(); Commented Mar 28, 2019 at 11:25

2 Answers 2

1
 Company.php     
    public function user() {
         return $this->belongsToMany('App\User', 'company_employees ', 'company_id', 
                    'user_id');
    }

    User.php
      public function position() {
            return $this->hasMany('App\Position');
        }


    $company = Company::with('user.position')->get();
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't solve a problem since user may have many positions. Although he can only have one position connected with a company.
Then it will return all user positions and not only those regarding company. $company->with('user.positions')->get() based on data from my question will resolve in company { User { position: { position at first company, position at second company } } }
0

I think you can only achieve it by using a custom with function.

Untested code:

$company = Company::first();
$company->employees()->with([
  'position' => function ($query) use ($company) {
    $query->where('company_id', $company->id);
  }
])->get();

Check out the documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.