Answer the question
In order to leave comments, you need to log in
Combining arrays into a collection?
Good afternoon, Dear community, I need your help, I can’t figure out the collections, in general the question is the following, there are several key => value arrays, the key is the same for everyone. How to collect all this into one collection in order to write it to the database. All data is brought to the desired form through the map function
$item_name= collect($nm)->map(function($item, $key){
return [
'c_id_item' => $key,
'name_item' => $item
];
})->toArray();
$gr_name= collect($gr)->map(function($item, $key) {
return [
'c_id_item' => $key,
'c_id_group_item' => $item
];
})->toArray();
$all_data= collect([]);
$all_data2= collect([]);
$all_data2= $all_data2->merge([$gr_name]);
$all_data2= $all_data2->merge([$item_name]);
$all_data= $all_data->merge($all_data2->toArray());
$collection = collect(['product_id' => 1, 'name' => 'Desk']); $merged = $collection->merge(['price' => 100, 'discount' => false]); $merged->all(); // ['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question