Php Laravel-与多对多相关的附加列
我有两张表-医生和科室,它们之间的关系医生和科室 我想添加一个布尔isBoss列,但我不知道如何更改其中的值并在laravel中引用它Php Laravel-与多对多相关的附加列,php,sql,laravel,voyager,Php,Sql,Laravel,Voyager,我有两张表-医生和科室,它们之间的关系医生和科室 我想添加一个布尔isBoss列,但我不知道如何更改其中的值并在laravel中引用它 我想做一个复选框,如果它被选中,它会将值更改为1。从你的问题来看,医生和科室之间似乎有很多关系,假设你有这些表 doctors(id, name ...) departments(id, name ...) department_doctor(id, doctor_id, department_id, is_boss ...) pivot table with
我想做一个复选框,如果它被选中,它会将值更改为1。从你的问题来看,医生和科室之间似乎有很多关系,假设你有这些表
doctors(id, name ...)
departments(id, name ...)
department_doctor(id, doctor_id, department_id, is_boss ...) pivot table with is_boss pivot field and default laravel naming convention
现在在模型中定义您的关系
医生模式
public function departments(){
return $this->hasMany('App\Department', 'department_doctor')->withTimestamps(); //if you have different pivot table name then replace department_doctor
}
public function doctors(){
return $this->hasMany('App\Doctor', 'department_doctor')->withPivot('is_boss')->withTimestamps(); //if you have different pivot table name then replace department_doctor
}
部门模式
public function departments(){
return $this->hasMany('App\Department', 'department_doctor')->withTimestamps(); //if you have different pivot table name then replace department_doctor
}
public function doctors(){
return $this->hasMany('App\Doctor', 'department_doctor')->withPivot('is_boss')->withTimestamps(); //if you have different pivot table name then replace department_doctor
}
现在插入
//get doctor id and is_boss from request
$doctorId = $request->input('doctor_id');
$isBoss = $request->input('is_boss');
//now save it using relation
$department = Department::find(1);
$department->doctors()->attach($doctorId, array('is_boss' => $isBoss));
有关详细信息,请从您的问题中查看,似乎您在医生和科室之间有很多关系,并且假设您有这些表格
doctors(id, name ...)
departments(id, name ...)
department_doctor(id, doctor_id, department_id, is_boss ...) pivot table with is_boss pivot field and default laravel naming convention
现在在模型中定义您的关系
医生模式
public function departments(){
return $this->hasMany('App\Department', 'department_doctor')->withTimestamps(); //if you have different pivot table name then replace department_doctor
}
public function doctors(){
return $this->hasMany('App\Doctor', 'department_doctor')->withPivot('is_boss')->withTimestamps(); //if you have different pivot table name then replace department_doctor
}
部门模式
public function departments(){
return $this->hasMany('App\Department', 'department_doctor')->withTimestamps(); //if you have different pivot table name then replace department_doctor
}
public function doctors(){
return $this->hasMany('App\Doctor', 'department_doctor')->withPivot('is_boss')->withTimestamps(); //if you have different pivot table name then replace department_doctor
}
现在插入
//get doctor id and is_boss from request
$doctorId = $request->input('doctor_id');
$isBoss = $request->input('is_boss');
//now save it using relation
$department = Department::find(1);
$department->doctors()->attach($doctorId, array('is_boss' => $isBoss));
有关详细信息,请检查它在数据透视表上添加了附加列后,请更新该关系,使其也包括该列。例如:
public function department()
{
return $this->belongsToMany('App\Department')
->withPivot('is_boss')
->withTimestamps();
}
及
然后可以按如下方式查询:
if $department->pivot->is_boss == 1
在数据透视表上添加了附加列后,请更新关系,使其也包含该列。例如:
public function department()
{
return $this->belongsToMany('App\Department')
->withPivot('is_boss')
->withTimestamps();
}
及
然后可以按如下方式查询:
if $department->pivot->is_boss == 1
向
科室
添加一个由医生id键入的附加关系(如科室老板
)是否更有意义?向科室
添加一个由医生id键入的附加关系(如科室老板
)是否更有意义?