2016-07-22 10:54
浏览 311


I have various models that I would like to combine into one (for output into a DataTables instance).

So far I have done this (in reality there are about 15 models, I'm just showing two here):

$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)

On first impressions this seemed to have done the trick well. However, I've noticed that the merge method combines records that share an ID value. So for example, the CarePlanUpdate with an ID of 12 is overridden by the MedicationError record with the ID of 12.

I have tried 'forgetting' the ID column but this doesn't seem to have any effect:

if($care_plan_updates) $all_records = $all_records->merge($care_plan_updates->forget('id'));

Ideally, I'd like a method that simply combines two collections without merging rows with duplicate values. I'm new to Eloquent - so any guidance on where I'm going wrong would be really appreciated!

图片转代码服务由CSDN问答提供 功能建议



  $ all_records = new 集合; 
 $ 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($  pharmaceutical_errors)$ all_records = $ all_records->合并($ 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的新手 - 所以任何有关我出错的指导都会非常感激!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题