【发布时间】:2019-03-25 12:38:05
【问题描述】:
我有开发自定义项目,其中有数据库表:
- 用户
- 候选人
- 公司
- 范围
- 技能
用户表:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('nickname')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('type')->default(0); // 0 = candidate and 1 = company
$table->rememberToken();
$table->timestamps();
});
候选人表:
Schema::create('candidates', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->string('name')->nullable();
$table->string('lastname')->nullable();
$table->string('surname')->nullable();
$table->text('about')->nullable();
$table->timestamps();
});
公司表:
Schema::create('companies', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->unsignedInteger('logo');
$table->foreign('logo')
->references('id')
->on('images')
->onDelete('cascade');
$table->string('name')->nullable();
$table->text('about')->nullable();
$table->string('website')->nullable();
$table->integer('employees')->nullable();
$table->timestamps();
});
范围表:
Schema::create('scopes', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
技能表:
Schema::create('skills', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
在这里,任何用户类型都有范围和技能。我可以通过创建一个表来解决此解决方案:
属性表:
Schema::create('attributes', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->morphs('model');
$table->timestamps();
});
属性表中的示例数据:
----------------------------------------
| id | user_id | model_id | model_type |
----------------------------------------
| 15 | 176 | 34458 | App\Scope |
----------------------------------------
| 29 | 245 | 17654 | App\Skill |
----------------------------------------
但是在这个解决方案中对我来说有一个不好的方面,这是在表中记录的每一行上重复模型的字符串名称。例如:
型号表:
Schema::create('models', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
注意: 可能无法使用 Model 的名称创建模块,因此您可能需要使用不同的名称来创建它,例如:参考
models 表中的示例数据:
-------------------
| id | name |
-------------------
| 10 | App\Scope |
-------------------
| 11 | App\Skill |
-------------------
创建models表后,attribute表的结构变为:
属性表:
Schema::create('attributes', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('model_id')
->references('id')
->on('models')
->onDelete('cascade');
$table->unsignedInteger('content_id');
$table->timestamps();
});
现在,我如何在模型内创建正确的关系,以获取候选人或公司用户类型的技能或范围?
我在用户模型中有自定义关系:
public function candidate()
{
return $this->hasOne(Candidate::class);
}
public function company()
{
return $this->hasOne(Company::class);
}
现在,我需要在 Candidate 和 Company 模型中这样的关系:
public function skills()
{
// Some code here
}
public function scopes()
{
// Some code here
}
通常像这样获得用户技能或范围:
$user->candidate->scopes
$user->company->skills
如果您对解决我的问题有其他建议,请提出您的建议。如果我在某处犯了错误,请在可能的情况下纠正我。
【问题讨论】:
-
您是否尝试过在候选人与范围以及公司与技能之间建立一对多的关系?候选人->hasMany(范围::类,'外键','主键');类似的公司和技能?
标签: database laravel database-design eloquent relationship