Php 这段关系怎么办,值得吗?(让所有科室去诊所)
我有三张桌子: 诊所Php 这段关系怎么办,值得吗?(让所有科室去诊所),php,laravel,laravel-5,laravel-5.6,Php,Laravel,Laravel 5,Laravel 5.6,我有三张桌子: 诊所 // second, third and fourth parameter could also be optional function departments(){ return $this->belongsToMany('App\Clinics', 'clinics_in_departments', 'department_id', 'clinic_id'); } $clinics = Clinics::with('departments')->
// second, third and fourth parameter could also be optional
function departments(){
return $this->belongsToMany('App\Clinics', 'clinics_in_departments', 'department_id', 'clinic_id');
}
$clinics = Clinics::with('departments')->whereIn('clinic_id',[1,2,3])->get();
部门
// second, third and fourth parameter could also be optional
function departments(){
return $this->belongsToMany('App\Clinics', 'clinics_in_departments', 'department_id', 'clinic_id');
}
$clinics = Clinics::with('departments')->whereIn('clinic_id',[1,2,3])->get();
各科室的诊所
// second, third and fourth parameter could also be optional
function departments(){
return $this->belongsToMany('App\Clinics', 'clinics_in_departments', 'department_id', 'clinic_id');
}
$clinics = Clinics::with('departments')->whereIn('clinic_id',[1,2,3])->get();
使用查询生成器:
$department = ClinicsInDepartment::whereIn('clinic_id',[1,2,3])
->join('departments', 'clinics_in_departments.department_id', '=', 'departments.id')
->get();
如何处理关系,这值得吗?您可以在Clinics模型中定义一个属于多个关系,如下面的代码所示
function departments(){
return $this->belongsToMany('App\Clinics', 'clinics_in_departments');
}
您可以在模型中定义一个属于多个关系,如下代码所示
function departments(){
return $this->belongsToMany('App\Clinics', 'clinics_in_departments');
}
如果您在多对多部分查看Laravel的文档,其中已经对其进行了解释。如果您打算继续使用Laravel,我建议您使用雄辩的最佳实践。对于其他开发人员来说,它更容易理解和阅读。尽你所能把你的产品做到最好总是值得的。它还提供了快速扩展和维护应用程序的可能性 您只需在模型诊所中定义一种关系
// second, third and fourth parameter could also be optional
function departments(){
return $this->belongsToMany('App\Clinics', 'clinics_in_departments', 'department_id', 'clinic_id');
}
$clinics = Clinics::with('departments')->whereIn('clinic_id',[1,2,3])->get();
检索您可以使用的数据
$clinics = Clinics::with('departments')->get();
// this would hold a list of departments for each clinic
要获得完全相同的数据,请将查询扩展到此
// second, third and fourth parameter could also be optional
function departments(){
return $this->belongsToMany('App\Clinics', 'clinics_in_departments', 'department_id', 'clinic_id');
}
$clinics = Clinics::with('departments')->whereIn('clinic_id',[1,2,3])->get();
因为这是一种多对多关系,所以您也可以为模型部门定义一种关系,并执行与上述完全相同的操作。如果您在多对多部分查看Laravel的文档,其中已经对其进行了解释。如果您打算继续使用Laravel,我建议您使用雄辩的最佳实践。对于其他开发人员来说,它更容易理解和阅读。尽你所能把你的产品做到最好总是值得的。它还提供了快速扩展和维护应用程序的可能性 您只需在模型诊所中定义一种关系
// second, third and fourth parameter could also be optional
function departments(){
return $this->belongsToMany('App\Clinics', 'clinics_in_departments', 'department_id', 'clinic_id');
}
$clinics = Clinics::with('departments')->whereIn('clinic_id',[1,2,3])->get();
检索您可以使用的数据
$clinics = Clinics::with('departments')->get();
// this would hold a list of departments for each clinic
要获得完全相同的数据,请将查询扩展到此
// second, third and fourth parameter could also be optional
function departments(){
return $this->belongsToMany('App\Clinics', 'clinics_in_departments', 'department_id', 'clinic_id');
}
$clinics = Clinics::with('departments')->whereIn('clinic_id',[1,2,3])->get();
因为这是一种多对多关系,所以您也可以为模型部门定义一种关系,并执行与上面提到的完全相同的操作。这可以通过多对多关系在eloquent中完成这可以通过多对多关系在eloquent中完成