I have an array in laravel named $destinations and the values are:
Collection {#391 ▼
#items: array:2 [▼
"Chicago, Illinois" => Collection {#398 ▼
#items: array:10 [▶]
}
"New York City, New York" => Collection {#403 ▼
#items: array:10 [▶]
}
]
}
This array is grouped by city name just like Chicago and New York, now I want to add one array item after the city name, it should be like this:
Collection {#391 ▼
#items: array:2 [▼
"Chicago, Illinois" => Collection {#398 ▼
#items: array:10 [▶]
"days" => "2"
}
"New York City, New York" => Collection {#403 ▼
#items: array:10 [▶]
"days" => "7"
}
]
}
I'm trying to use array_merge when i loop the array, I use foreach($destinations as $key => $destination) in the looping, basically the $key will return increment number like 0, 1, 2, etc but in this case it return the city name. Anyone knows how to do it? or how to make the $key still increment number after grouped by in laravel?
As I see your $destinations variable is a collection. So you can iterate through it using each method.
for example:
$destinations = $destinations->each(function ($item, $key) {
// You can modify $item here
// $item['days'] = <no_of_days>;
});
To get the increment number, you can do something like this:
$element_position = 0;
$destinations = $destinations->each(function ($item, $key) use ($element_position) {
// You can modify $item here
// $item['days'] = <no_of_days>;
// You can get the $element_position here
// Iterate $element_position
$element_position++;
});
Related
i want to sort by cu_customer_shortname in nested array collection but seems i cant see any ways do it . here is my example of my array list
{#1811 ▼
+"co_group_id": "ADUKO"
+"co_description": "ADUKO"
+"co_customer_id": "038"
+"co_active_sts": "1"
+"irel__h_d_rel_group_customer": array:3 [▼
0 => {#1812 ▼
+"ID": "5"
+"gc_group_id": "ADUKO"
+"gc_customer_id": "038"
+"irel__h_d_customer_master": {#1813 ▶}
}
1 => {#1814 ▼
+"ID": "6"
+"gc_group_id": "ADUKO"
+"gc_customer_id": "044"
+"irel__h_d_customer_master": {#1815 ▼
+"cu_customer_ID": "044"
+"cu_customer_Shortname": "JPEN" <------ Sort this shortname
}
}
]
Here is my code that i already tried
$groupList = $contents->data->groupList;
$order = $groupList->irel__h_d_rel_group_customer; //arrayList
$collection = new \Illuminate\Support\Collection($groupList); //collection
$sorted = $collection->SortByDesc('irel__h_d_customer_master.cu_customer_Shortname'); // sorted by cu_customer_shortname
My code not return any error but my list remain and not sorted by cu_customer_Shortname
Is there any laravel collection methold that i dont know of, which would allow me to sort an array based on the number of elements on the sub array?
Illuminate\Support\Collection {#1143 ▼
#items: array:7 [▼
"A" => Illuminate\Support\Collection {#21181 ▼
#items: array:10 [▶]
}
"B" => Illuminate\Support\Collection {#21182 ▼
#items: array:8 [▶]
}
"C" => Illuminate\Support\Collection {#21183 ▼
#items: array:9 [▶]
}
"D" => Illuminate\Support\Collection {#21184 ▼
#items: array:5 [▶]
}
"E" => Illuminate\Support\Collection {#21185 ▼
#items: array:2 [▶]
}
"F" => Illuminate\Support\Collection {#21186 ▼
#items: array:4 [▶]
}
"G" => Illuminate\Support\Collection {#21187 ▼
#items: array:15 [▶]
}
]
}
I could do something like THIS using usort() but I was just wondering if there exists any method within laravel collections, which I yet dont know or may be I am not able to locate it within Laravel Collections.
I don't know if anyone is going to stumble into similar problem, I found a way around it as mentioned in the documentation
I still don't know if this is the perfect way of doing it, but it did the trick for me. I am just posting it such that it might help someone a lot of headache and time.
I would still love to hear other answers and comments on the alternative ways of doing it.
$sorted = $mostWatchedVideosThisWeek->sortByDesc(function ($stats, $key) {
return count($stats);
});
if you searching for usort(), Laravel https://laravel.com/docs/5.8/collections#method-sort work same like usort()
eg:
$full_sorted = $collection_data->sort(function($a ,$b) { //$a first element ,$b second element
if ((count($a) > count($b))) {
return -1;
}else{
return 1;
}
})->values();
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 two Laravel collections. first collection $customer(It has 30 elements):
Collection {#2615 ▼
#items: array:31 [▼
0 => {#2610 ▼
+"allocated_date": "2016-12-01"
+"Customer": "44"
}
1 => {#2616 ▼
+"allocated_date": "2016-12-02"
+"Customer": "42"
}
And other one is $agent(It has 17 elements)
Collection {#2586 ▼
#items: array:16 [▼
0 => {#2585 ▼
+"agent_allocated_date": "2016-12-01"
+"Agent": "41"
}
1 => {#2587 ▼
+"agent_allocated_date": "2016-12-02"
+"Agent": "95"
}
I need the result like this (leftJoin allocated_date with agent_allocated_date). can not using merge or combine. because number of elements in both collections are different. help me to find the output
array:31 [▼
0 => {#2596 ▼
+"allocated_date": "2016-12-01"
+"Customer": "44"
+"agent_allocated_date": "2016-12-01"
+"Agent": "41"
}
You need to map over the customer and find the agent for that customer and merge the two collections:
$customer= $customer->map(function ($item, $key) {
$single_agent = $agent->where('agent_allocated_date',$item->agent_allocated_date);
return collect($item)->merge($single_agent);
});
The better approach to execute join query.
I have tested this join query. It's working. Try this one.
$data = DB::table('customer')->select('customer.allocated_date','customer.Customer','agent.agent_allocated_date','agent.Agent')->join('agent','agent.id','=','customer.id');
print_r($data);
I have the following Eloquent query in a Laravel 5.2 project:
$regsByCtryCollection = Organisation::join('countries_currencies', 'countries_currencies.id', '=', 'organisations.country_id')
->select(DB::raw('DISTINCT LCASE(countries_currencies.country_code) AS ctry, COUNT(organisations.id) AS val'))
->groupBy('ctry')
->get();
The raw query produces this output:
ctry val
at 1
au 5
br 1
The Eloquent call produces a collection of three rows (matching raw query output) like this:
Collection {#791 ▼
#items: array:3 [▼
0 => Organisation {#777 ▼
#table: "organisations"
#hidden: []
........
#attributes: array:2 [▶]
#original: array:2 [▼
"ctry" => "at"
"val" => 1
]
#relations: array:5 [▶]
........
}
1 => Organisation {#778 ▶}
2 => Organisation {#779 ▶}
]
}
I then pluck the values and format for Highmaps like this
$regsByCtry = $regsByCtryCollection->pluck('ctry', 'val')->map(function($country, $value) {
return [
"hc-key" => $country,
"value" => $value
];
})->values()->toJson();
And one of the values is dropped and I get this:
[
{"hc-key":"br","value":1},
{"hc-key":"au","value":5}
]
Why is the first entry getting dropped?
{"hc-key":"at","value":1}
I am using this same process with two other Eloquent queries and it works as expected, but just not on this collection.
Additionally, I also sum all the values in the array of objects like this:
$regsTotal = array_sum($regsByCtryCollection->pluck('val')->toArray());
And I get the correct value, including all three records summed:
$regsTotal = 7;
The issue is with pluck('ctry', 'val'). This will return val as key & ctry as value. In your query output at & br has same value 1. So one of it getting replaced by the other one.
Try pluck('val', 'ctry')->map(function($value, $country)
Reference