Using Laravel 4.2 Have the below datastructure retrieved by using Eloquent queries.
[
{
Id: 1,
Product:Test1,
TypeId:100
}
{
Id: 2,
Product:Test2,
TypeId:200
}
{
Id: 3,
Product:Test2,
TypeId:200
}
];
Want to create a hash map based on TypeId as key so I can search the products easily. Want the final output below way
{
'100' : {
Id: 1,
Product:Test1,
TypeId:100
},
'200' : [
{
Id: 2,
Product:Test2,
TypeId:200
},
{
Id: 3,
Product:Test2,
TypeId:200
}
]
}
Tried the below eloquent function but duplicates in the Type Id 200 are ignored.
$type_id_hash=$fm->fmmodelitems->keyBy('TypeId');
For typeid 200 only one record exists when using keyby function as it ignore duplicates. I want to search array of hashmap for a key like typeid. How can it be done.
Perhaps you want
groupBy
onCollection
:This should group them under that 'key'.