Laravel collection: flatten method ignored - php

Possibly an obvious one, but here it is:
After dd'ing a query result, I have:
$items = DB::table('my_table')->get(['id']);
dd($items);
Which results in:
Collection {#228 ▼
#items: array:1 [▼
0 => {#227 ▼
+"id": 2
}
]
}
Then, when I try to flatten it, it ignores me:
dd($items->flatten());
Resulting in:
Collection {#209 ▼
#items: array:1 [▼
0 => {#227 ▼
+"id": 2
}
]
}
Shouldn't I receive something like the flattened version of the collection?
How can I do it?
Thanks in advance.

You need to check it like
$flattened = $items->flatten();
dd($flattened->all());
// or dd($items->flatten()->all());
Source from official documentation.
If you want to fetch id and completely flatten then use
$items = DB::table('my_table')->pluck('id');
dd($items);
Here is the pluck link.

Related

Laravel WhereIn & With Query merge

so I’m running this query when a user selects multiple categories on the frontend of the site, I want it to bring back the products that are related to the selected categories… this works, however, I have 2 collections that I need to merge together. Here is the code:
$category = Category::whereIn('permalink', $request->get('categoryFilter'))->with('products')->get()->pluck('products');
I get the following back:
Illuminate\Support\Collection {#1779 ▼
#items: array:2 [▼
0 => Illuminate\Database\Eloquent\Collection {#1780 ▼
#items: []
#escapeWhenCastingToString: false
}
1 => Illuminate\Database\Eloquent\Collection {#1799 ▼
#items: array:24 [▶]
#escapeWhenCastingToString: false
}
]
#escapeWhenCastingToString: false
}
The first category has no products but the second has, is there a way of merging these 2 together?
Try this,
$category = Products::whereIn('category_id',Category::whereIn('permalink',
$request->get('categoryFilter')
)->pluck('id')
)->get();

Sort Laravel collection by value

I have got an array that looks like this:
^ Illuminate\Support\Collection {#2057 ▼
#items: array:2 [▼
"pizza" => 2
"cookking vuurschaal palma" => 1
]
}
does anyone know how to sort by value instead of keys in Laravel?
Collection::sortDesc(); worked for me

Laravel sort array according to number of elements in sub array

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();

Merge 2 Collections or Arrays by Key

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.

Join two laravel 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);

Categories