【发布时间】:2016-07-22 10:54:32
【问题描述】:
我想将各种模型合并为一个(用于输出到 DataTables 实例中)。
到目前为止,我已经这样做了(实际上大约有 15 个模型,我这里只展示两个):
$all_records = new Collection;
$care_plan_updates = \App\CarePlanUpdate::where('resident_id', $resident->id)->where('closed','0')->get();
if($care_plan_updates) $all_records = $all_records->merge($care_plan_updates);
$medication_errors = \App\MedicationError::where('resident_id', $resident->id)->where('closed','0')->get();
if($medication_errors) $all_records = $all_records->merge($medication_errors);
return Datatables::of($all_records)
在第一印象中,这似乎做得很好。但是,我注意到 merge 方法合并了共享 ID 值的记录。例如,ID 为 12 的 CarePlanUpdate 被 ID 为 12 的 MedicationError 记录覆盖。
我尝试过“忘记” ID 列,但这似乎没有任何效果:
if($care_plan_updates) $all_records = $all_records->merge($care_plan_updates->forget('id'));
理想情况下,我想要一种简单地组合两个集合而不合并具有重复值的行的方法。我是 Eloquent 的新手 - 所以任何关于我哪里出错的指导都将非常感激!
【问题讨论】:
标签: php laravel laravel-5 eloquent