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

gurinder's avatar

Laravel 5.1 Polymorphic relationship Or Regular Relationship -- need help

I have Three model in my code Company, User and Employee

User
 id
 name

Company
 id
 name

Employee
 employer_id // foreign key to id on company table
 employable_id
 employable_type
  • User can have many companies as owner/director/president
  • User Cannot have Any employee directly
  • Company can have only one user as owner/director/president
  • Company can have many employees
  • Employee can have one employer which is company
  • Employee can have many user or company as an employee -employer is always a company
  • a company as an employer, can hire another company as an employee or a user as an employee...
  • so company can have two relation with employee table that is as an employee itself and as an employer.
  • user are not to allowed to employeed anybody directly
  • user can hire employee through a company only ..

I am not able to figure out what kind of relationship i can use here so that i can have user and company as an employee but also company as an employer.

-- Edit I have the following table structures:

Employee Table ->
        employer_id' // Only company_id
        employee_person_id // user_id
        employee_company_id // Company_id as an employee
        employee_type // company or person
        name // name of company acting as employee or user full name
        email // email of company acting as employee or user email

        $table->foreign('employer_id')
            ->references('id')->on('companies')
            ->onDelete('cascade');

        $table->foreign('employee_person_id')
            ->references('id')->on('users')
            ->onDelete('cascade');

        $table->foreign('employee_company_id')
            ->references('id')->on('companies')
            ->onDelete('cascade');

My employee class has following methods

class Employee extends Model 
{
   public function employedBy()
   {
     return $this->belongsTo(\App\Models\Company::class, 'employer_id');
   }

   public function employedPerson()
   {
     return $this->belongsToMany(\App\Models\User::class,   'employee_person_id');
    }

   public function employedCompany()
   {
      return $this->belongsToMany(\App\Models\Company::class, 'employee_company_id');
    }
}

My Company has following methods

class Company extends Method
{
  public function employees()
  {
    return $this->hasMany(\App\Models\Employee::class, 'employer_id');
  }

  public function employers()
  {
    return $this->belongsToMany(\App\Models\Employee::class, 'employee_company_id');
  }
}

My User has following methods

class User extends Method
{
  public function companies()
  {
    return $this->hasMany(\App\Models\Company::class, 'user_id');
  }

  public function employers()
  {
    return $this->belongsToMany(\App\Models\Employee::class, 'employee_person_id');
  }
}

Ok now let me explain in steps:

  1. Company wants to hire employee ( employee can be any other company or a person)
  2. Company search employee by email
  3. Searched company or person added to the employee table

--Also when i query for all the employees i want to have both employed users and employed company in collection, please suggest me the way to fetch list joining users and companies through employee table--

// I want to write something like this
$company->getEmployees()  = // to get detail list of employees and companies in one single collection


                
                
0 likes
1 reply
T2thec's avatar

If you think about it, a user is an employer right? A user and an employee being a person.

If this is correct with what you are trying to create then is an 'hasOne' on your user and a 'hasMany' on your company. IE, a user/employee/person can have one company.

Again, depending on what you are building, you may want to have a 'belongsToMany' on your user model. You could add an extra field n your pivot for current. That way your users can have many employers, but only one that is current. The rest are previous jobs.

Does tha make sense? It depends on what you are trying to build, but really, an employee is a user in most cases. If your employees aren't logging in, then your logic might be right and what I have mentioned here is crap.

Please or to participate in this conversation.