Php 如何在Laravel中组合多个雄辩的收藏
我有各种各样的模型,我想将它们组合成一个(用于输出到DataTables实例) 到目前为止,我已经做到了这一点(实际上大约有15个模型,我在这里只展示了两个): 乍一看,这似乎很成功。但是,我注意到merge方法合并了共享ID值的记录。例如,ID为12的CarePlanUpdate被ID为12的MedicationError记录覆盖 我已尝试“忘记”ID列,但这似乎没有任何效果:Php 如何在Laravel中组合多个雄辩的收藏,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,我有各种各样的模型,我想将它们组合成一个(用于输出到DataTables实例) 到目前为止,我已经做到了这一点(实际上大约有15个模型,我在这里只展示了两个): 乍一看,这似乎很成功。但是,我注意到merge方法合并了共享ID值的记录。例如,ID为12的CarePlanUpdate被ID为12的MedicationError记录覆盖 我已尝试“忘记”ID列,但这似乎没有任何效果: if($care_plan_updates) $all_records = $all_records->mer
if($care_plan_updates) $all_records = $all_records->merge($care_plan_updates->forget('id'));
理想情况下,我想要一个方法,它可以简单地组合两个集合,而不合并具有重复值的行。我是一个新的口才-所以任何关于我错在哪里的指导都会非常感激 我找到了解决办法。虽然我不确定这是实现我目标的最优雅的方式,但它确实做到了: 我没有使用
merge
收集方法,而是决定循环每个单独的收集,并使用push
将每个记录添加到我的主收集中。像这样:
$all_records = new Collection;
$care_plan_updates = \App\CarePlanUpdate::where('resident_id', $resident->id)->where('closed','0')->get();
foreach($care_plan_updates as $care_plan_update) {
$all_records->push($care_plan_update);
}
$risk_assessment_updates = \App\RiskAssessmentUpdate::where('resident_id', $resident->id)->where('closed','0')->get();
foreach($risk_assessment_updates as $risk_assessment_update) {
$all_records->push($risk_assessment_update);
}
$all_records = new Collection;
$care_plan_updates = \App\CarePlanUpdate::where('resident_id', $resident->id)->where('closed','0')->get();
foreach($care_plan_updates as $care_plan_update) {
$all_records->push($care_plan_update);
}
$risk_assessment_updates = \App\RiskAssessmentUpdate::where('resident_id', $resident->id)->where('closed','0')->get();
foreach($risk_assessment_updates as $risk_assessment_update) {
$all_records->push($risk_assessment_update);
}