vendredi 8 janvier 2016

Merge two models grouped by month

I have to models and they are grouped by months:

$cars= $company->car->groupBy(function($item)
{
     return Carbon::parse($item->created_at)->format('M');
});

$boats= $company->boat->groupBy(function($item)
{
     return Carbon::parse($item->created_at)->format('M');
});

So I got two collection

  Collection {#841 ▼
  #items: array:3 [▼
    "Jan" => Collection {#848 ▼
      #items: array:1 [▼
        0 => Cars {#859 ▶}
      ]
    }
    "Fab" => Collection {#852 ▶}
    "Mar" => Collection {#853 ▶}
  ]
}

And the same one for the boats, my question is how to merge them to get something like:

  Collection {#841 ▼
  #items: array:3 [▼
    "Jan" => Collection {#848 ▼
      #items: array:1 [▼
        0 => Cars {#859 ▶}
        1 => Boats {#860 ▶}
      ]
    }
    "Fab" => Collection {#852 ▶}
    "Mar" => Collection {#853 ▶}
  ]
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire