Problem is how do i print collections into view. and first div only print Collection 0 and next one Collection ++ like this
ExamsController#create
$collections = $questions->split(5);
$collections->toArray();
dd($collections);
dd($collections);
Collection {#455 ▼
#items: array:5 [▼
0 => Collection {#394 ▶}
1 => Collection {#619 ▶}
2 => Collection {#407 ▶}
3 => Collection {#398 ▶}
4 => Collection {#275 ▶}
]
}
Create.blade.php
<div>#foreach($collections as $collection)
Collection 0
#endforeach</div>
<div>#foreach($collections as $collection)
Collection 1
#endforeach</div>
<div>#foreach($collections as $collection)
Collection 2
#endforeach</div>
<div>#foreach($collections as $collection)
Collection 3
#endforeach</div>
<div>#foreach($collections as $collection)
Collection 4
#endforeach</div>
Use index to fetch particular collections
#foreach($collections[0] as $collection) //here $collection is Collection 0
Related
i have a collection named detailed as below :
Collection {#1421 ▼
#items: array:2 [▼
3943 => Collection {#1419 ▼
#items: array:2 [▼
0 => RoomPricingHistory {#923 ▶}
1 => RoomPricingHistory {#1042 ▶}
]
}
3944 => Collection {#1420 ▼
#items: array:2 [▼
0 => RoomPricingHistory {#1153 ▶}
1 => RoomPricingHistory {#1264 ▶}
]
}
]
}
now i want to get the sum of RoomPricingHistory for 3943 item and 3944 ofc it can be more of 2 item so i want to get the sum of each collection how can i achieve that ??
The Collection sum method can take a callback so you can define what is going to be calculated. In this case you can call sum on the main collection and in the callback which will give you access to the internal collections, also call sum.
$detailed->sum(function ($group) {
return $group->sum('sales_price');
});
Laravel 6.x Docs - Collections - Available Methods - sum
Since that isn't what you are looking for, you can use something like mapWithKeys to go through the collection and call sum for the groups to get the sum for each group:
$sums = $detailed->mapWithKeys(function ($group, $key) {
return [$key => $group->sum('sales_price')];
});
Laravel 6.x Docs - Collections - Available Methods - mapWithKeys
I'm trying my best to prevent from looping over a collection to get more data that I need for a data table. I need a way to combine these 2 collections or arrays.
I have 2 functions inside a model that calls an API that gives me an array of data that I convert into a collection.
Orders()
public function orders(){
$orders_array = $this->api()->request('orders.json');
return collect($orders_array);
}
Returns:
Collection {#274 ▼
#items: array:2 [▼
0 => {#282 ▼
+"id": 628602961977
}
1 => {#270 ▶}
order_meta()
public function order_meta($order_id){
$order_metafields_array = $this->api()->request('meta.json');
return collect($order_metafields_array);
}
Returns:
Collection
#items: [
+"name": "meta_data"
]
What I am wanting to do is inject the order_meta() into each order using the order ID.
I guess I am wanting to do something like this:
public function orders_detailed(){
$orders = $this->orders();
$keyed = $orders->mapWithKeys(function ($item) {
return [$item['meta'] => $this->order_metafields($item['id'])];
});
}
In hopes it will return:
Collection {#274 ▼
#items: array:2 [▼
0 => {#282 ▼
+"id": 628602961977
+meta => {
+"name":"meta_data"
}
}
1 => {#270 ▶}
Is something like this possible? I'm also open to merging it in as an array first, then creating a collection.
I have a search result from image model and $photo saved the data which $photo->type == 'photo'.
$photo = $image->filter(function($photo,$key) use($path){
if($photo->type == 'photo'){
$photo->url = $path.$photo->image;
return $photo;
}
});
Here is the $photo collection and is there any way to array_values() the items data?
Collection {#352 ▼
#items: array:3 [▼
2 => ImageBanquet {#349 ▶}
3 => ImageBanquet {#350 ▶}
4 => ImageBanquet {#351 ▶}
]
}
Check values() collection helper.
$values = $collection->values();
collection is obtrained like this
Collection {#241 ▼
#items: array:2 [▼
"Oct-2016" => Collection {#225 ▼
#items: array:1 [▼
0 => Event {#243 ▶}
]
}
"Nov-2016" => Collection {#236 ▼
#items: array:2 [▼
0 => Event {#244 ▶}
1 => Event {#245 ▶}
]
}
]
}
i want to show data like .
Oct 2016
event 1
Nov 2016
event 1
event 2
i am new in laravel kindly help me how to show collection key in title Oct-2016 and list of each key collection in blade
Laravel collections implement php's iterable functionality. In other words, you can just use foreach loops. This will give you the basic idea.
foreach ($collection as $month => $events)
{
var_dump($month);
foreach ($events as $event)
{
var_dump($event);
}
}
I have this collection:
Collection {#604 ▼
#items: array:4 [▼
0 => CarsMark {#596 ▶}
1 => CarsMark {#594 ▶}
2 => CarsMark {#594 ▶}
3 => CarsMark {#595 ▶}
]
}
As you can see there is two the same items (594). How can i retrieve all items from with collection without the same items?
Use the collection's unique method:
$unique = $collection->unique();