Reputation: 974
I've 3 models in my app Company, User, Role
The business rule is like this:
I'm thinking about creating a 4th model, something like Employees.
But I don't have any idea how to create a migration and define relationship between these models.
Upvotes: 1
Views: 1362
Reputation: 64476
I would also go for 4 tables as
First three tables will save the regular data without holding any foreign key from other tables, 4 table will related these tables to each other with following attributes
Now your owing side tables which are users , roles and companies will define a relationship with user_role_company as hasMany
and user_role_company
will define inverse relation with these 3 tables as belongsTo
class User extends Model{
public function userRoleCompany()
{
return $this->hasMany('App\UserRoleCompany', 'user_id','id');
}
}
class Role extends Model{
public function userRoleCompany()
{
return $this->hasMany('App\UserRoleCompany', 'role_id','id');
}
}
class Company extends Model{
public function userRoleCompany()
{
return $this->hasMany('App\UserRoleCompany', 'company_id','id');
}
}
class UserRoleCompany extends Model{
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
public function role()
{
return $this->belongsTo('App\Role', 'role_id');
}
public function company()
{
return $this->belongsTo('App\Company', 'company_id');
}
}
Upvotes: 3