Home > Back-end >  laravel collections merging and combining with two different data sets
laravel collections merging and combining with two different data sets

Time:07-03

how do I merge a collection if the number items in the collection are not equal? I tried mapping, merging, etc. It's not getting the results I want. $vitals = $animal->getLatestVitals();

    // Get the conditions based on the animal's vitals
    if($vitals)
    {
        $vitalIds = $vitals->pluck('vital_id', 'value')->toArray();

        $conditionVitals = DB::table('condition_vital')
            ->whereIn('vital_id', $vitalIds)->get();

        $collections = $conditionVitals->merge($vitals);


        dd($collections);
        $filtered = $collections->filter(function ($value, $key) {
            if($value->value){
                return $value;
            }
        });

    Illuminate\Support\Collection {#1704 ▼
  #items: array:7 [▼
    0 => {#1698 ▼
       "condition_id": 51
       "vital_id": 2
       "min": "6"
       "max": "9"
    }
    1 => {#1753 ▼
       "condition_id": 95
       "vital_id": 3
       "min": "3"
       "max": "4"
    }
    2 => {#1748 ▼
       "id": "anim_1656735049BzJoGENzpBfip43"
       "name": "weight"
       "value": "80"
       "created_at": "2022-08-14 00:00:00"
       "animal_id": "ani_1656730205DMhSBgby671VEdazd"
       "vital_id": 1
    }
    3 => {#1747 ▼
       "id": "anim_1656735049oDpMUQ5PJkKdVS8ddd"
       "name": "body_score"
       "value": "8"
       "created_at": "2022-07-14 00:00:00"
       "animal_id": "ani_1656730205DMhSBgby671VEdazd"
       "vital_id": 2
    }
    4 => {#1744 ▼
       "id": "anim_1656739859JliRP0IawXz987J"
       "name": "dental_score"
       "value": "2"
       "created_at": "2022-09-01 00:00:00"
       "animal_id": "ani_1656730205DMhSBgby671VEdazd"
       "vital_id": 3
    }
 
  ]
  #escapeWhenCastingToString: false
}

I've merged the collections, now I need them to combine the the array if the vital ID matches and with the min/max values. Something like this: I don't need any items that doesn't contain the min and max value--but I do need it to merge together.

   Illuminate\Support\Collection {#1704 ▼
      #items: array:7 [▼
  
        1 => {#1747 ▼
           "id": "anim_1656735049oDpMUQ5PJkKdVS8ddd"
           "name": "body_score"
           "value": "8"
           "created_at": "2022-07-14 00:00:00"
           "animal_id": "ani_1656730205DMhSBgby671VEdazd"
          "condition_id": 51
           "vital_id": 2
           "min": "6"
           "max": "9"
        }
        2 => {#1744 ▼
           "id": "anim_1656739859JliRP0IawXz987J"
           "name": "dental_score"
           "value": "2"
           "created_at": "2022-09-01 00:00:00"
           "animal_id": "ani_1656730205DMhSBgby671VEdazd"
           "condition_id": 95
           "vital_id": 3
           "min": "3"
           "max": "4"
        }
        }
     
      ]
      #escapeWhenCastingToString: false
    }

CodePudding user response:

You don't need to combine them in first place, you can achieve this by creating a relation in model and then calling it in query builder, assuming you have a model for Condition Vitals, you can do something like this:

ConditionVitals.php Model

function vital(){
    return $this->hasOne(Vital::class);
}

and then call this relation on your query using with() function like this:

ConditionVitals::whereIn('vital_id', $vitalIds)->with('vital')->get();

what this will do is give you a nice and clean collection like this:

{
  "id": "anim_1656735049BzJoGENzpBfip43"
  "name": "weight"
  "value": "80"
  "created_at": "2022-08-14 00:00:00"
  "animal_id": "ani_1656730205DMhSBgby671VEdazd"
  "vital_id": 1
  "vital:{
     "condition_id": 51
     "vital_id": 1
     "min": "6"
     "max": "9"
    }
}

This way only those records will be fetched that are required instead of the whole table with non matching IDs. The similar way you can create belongsTo() relationship. But make sure those IDs are constrained to avoid any kind of exceptions since the relation won't work otherwise. Get help here

  • Related