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:
- Company wants to hire employee ( employee can be any other company or a person)
- Company search employee by email
- 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