tbl*_*cog 4 php mysql pivot-table eloquent laravel-5
我正在尝试在 Laravel 5 中创建一个应用程序来跟踪患者的体检,以便我可以创建患者历史记录,我使用 Eloquent 作为我的模型抽象推荐。
为此,我使用 laravel 迁移创建了三个表和一个数据透视表,如下图所示:

用户可以是医生或管理员,他们为他们所做的特定考试输入患者的分数结果,例如我有一个数据透视表,其中包含三个外键来关联三个实体:
在 Eloquent 中,我按照 Laravel 文档多对多创建了这些多对多模型,以支持提议的业务逻辑:
<?php
// Patient.php
class Patient extends Model
{
public function exams() {
return $this->belongsToMany('App\Exam', 'exam_patient');
}
}
// Exam.php
class Exam extends Model
{
public function patients()
{
return $this->belongsToMany('App\Patient', 'exam_patient');
}
public function users()
{
return $this->belongstoMany('App\User', 'exam_patient');
}
}
// User.php
class User extends Model
{
public function exams() {
return $this->belongsToMany('App\Exam', 'exam_patient');
}
}
Run Code Online (Sandbox Code Playgroud)
在 tinker 中,我尝试在进行重大更改之前模拟一个常见用例:医生对患者进行检查:
>>> $user= App\User::first()
=> App\User {#671
id: "1",
name: "Victor",
email: "",
created_at: "2015-10-10 18:33:54",
updated_at: "2015-10-10 18:33:54",
}
>>> $patient= App\Patient::first()
=> App\Patient {#669
id: "1",
username: "",
name: "Tony",
lastname: "",
birthday: "0000-00-00",
created_at: "2015-10-10 18:32:56",
updated_at: "2015-10-10 18:32:56",
}
>>> $exam= App\Exam::first()
=> App\Exam {#680
id: "1",
name: "das28",
title: "Das28",
created_at: "2015-10-10 18:31:31",
updated_at: "2015-10-10 18:31:31",
}
>>> $user->save()
=> true
>>> $patient->save()
=> true
>>> $exam->save()
=> true
Run Code Online (Sandbox Code Playgroud)
问题是当我尝试链接(或附加)至少两个模型时,出现以下错误:
>>> $patient->exams()->attach(1)
Illuminate\Database\QueryException with message
'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a
child row: a foreign key constraint fails (`cliniapp`.`exam_patient`, CONSTRAINT
`exam_patient_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
ON DELETE CASCADE) (SQL: insert into `exam_patient` (`exam_id`, `patient_id`)
values (1, 1))'
Run Code Online (Sandbox Code Playgroud)
如果我什至不能附加其中两个,我该如何链接这三个模型?修补匠有什么方法可以批量附加它们,以便我可以在我的数据透视表(exam_patient)中将它们关联起来,而 MySql 没有显示该错误,或者如果我的方法有误?非常感谢任何帮助。
当您的多对多关系中有额外的字段时,您必须在声明关系时“注册”它们,如上述链接的检索中间表列部分所示。
所以你应该总是声明你的“额外”数据透视表,根据你定义关系的位置而不同:
class Patient extends Model
{
public function exams()
{
return $this->belongsToMany('App\Exam', 'exam_patient')->withPivot('user_id');
}
}
class Exam extends Model
{
public function patients()
{
return $this->belongsToMany('App\Patient', 'exam_patient')->withPivot('user_id');
}
public function users()
{
return $this->belongstoMany('App\User', 'exam_patient')->withPivot('patient_id');
}
}
class User extends Model
{
public function exams()
{
return $this->belongsToMany('App\Exam', 'exam_patient')->withPivot('patient_id');
}
}
Run Code Online (Sandbox Code Playgroud)
在附加/分离部分,您可以看到您可以像这样设置这些额外的字段:
$patient->exams()->attach($exam->id, ['user_id' => $user->id]);
Run Code Online (Sandbox Code Playgroud)
您的问题是,当您创建患者/检查关系而不为 user_id 列传递数据时,exam_patient 表上的 user_id 外键约束失败,因此上述方法将起作用。
如果您不希望始终像那样传递 user_id,无论出于何种原因,您还可以使该 user_id 列在数据库上可以为空。
当然,可以说在这里使用两个数据透视表(exam_patient 和exam_user),或者使用多对多的多态关系会是更好的方法。
正如我们从关于多态关系的Laravel 文档中看到的那样,为了在此处实现它们,我们将删除 exam_patient 表作为可检查表,该表将有 3 列:exam_id、examable_id 和examable_type。
模型看起来像:
class Patient extends Model
{
public function exams()
{
return $this->morphToMany('App\Exam', 'examable');
}
}
class Exam extends Model
{
public function patients()
{
return $this->morphedByMany('App\Patient', 'examable');
}
public function users()
{
return $this->morphedByMany('App\User', 'examable');
}
}
class User extends Model
{
public function exams()
{
return $this->morphToMany('App\Exam', 'examable');
}
}
Run Code Online (Sandbox Code Playgroud)
要完全更新可检查表,您需要执行以下操作:
$exam->patients()->save($patient);
$exam->users()->save($user);
Run Code Online (Sandbox Code Playgroud)
所以它是两个查询而不是一个,你失去了外键关系并且“examables”是一个尴尬的名字。
The argument for would be that the relationships are a bit more declarative and it's easier to handle a single exam for a single patient belonging to multiple users/doctors.