How do I add new item with key to collection in Laravel? - php

I have a Laravel collection $Data. When I code outputs
dd($Data); // it outputs the following
Illuminate\Support\Collection {#1344 ▼
#items: array:3 [▼
0 => {#1342 ▶}
1 => {#1334 ▶}
2 => {#1346 ▶}
]
}
$Data->push(['Total'=>600]);
dd($Data);
When I push a Total, it does inserts but output is not as expected.
Illuminate\Support\Collection {#1344 ▼
#items: array:4 [▼
0 => {#1342 ▶}
1 => {#1334 ▶}
2 => {#1346 ▶}
3 => array:1 [▼
"Total" => 600
]
]
}
How do I get 3 => {#1353▶) ?

You're pushing an Array to $Data, while the other items in that Collection are Objects (stdClass or similar). To make this consistent, use casting:
$Data->push((object)['Total' => 600]);
Now you should see 3 => {#...} instead of 3 => array:1.

Related

how to change the format of a array to collection as each item in laravel

i have an array of collections like below :
array:9 [▼
0 => Collection {#990 ▶}
1 => Collection {#1109 ▶}
2 => Collection {#1221 ▶}
3 => Collection {#1331 ▶}
4 => Collection {#1442 ▶}
5 => Collection {#1554 ▶}
6 => Collection {#1664 ▶}
7 => Collection {#1775 ▶}
8 => Collection {#1887 ▶}
]
i want to make this a single collection and make each collection as 1 item of that collection now what i tried is collect($f) but i get the result as below :
Collection {#1443 ▼
#items: array:9 [▼
0 => Collection {#990 ▶}
1 => Collection {#1109 ▶}
2 => Collection {#1221 ▶}
3 => Collection {#1331 ▶}
4 => Collection {#1442 ▶}
5 => Collection {#1554 ▶}
6 => Collection {#1664 ▶}
7 => Collection {#1775 ▶}
8 => Collection {#1887 ▶}
]
}
now i want to know how can i make this 1 collection and make all those 8 collection as items of that collection like below :
Collection {#990 ▼
#items: array:1 [▼
0 => RoomPricingHistory {#971 ▶}
1 => RoomPricingHistory {#971 ▶}
2 => RoomPricingHistory {#971 ▶}
3 => RoomPricingHistory {#971 ▶}
4 => RoomPricingHistory {#971 ▶}
]
}
thanks
Once you have a collection of collections, you can use flatten to get all elements of the underlying collections in the parent collection.
collect($f)->flatten(1);
I'm not sure if this is what you're after.
First, using artisan I'll make a collection of collections. Each collection has a single element array [1]
$ php artisan tinker
>>> $a = collect(1)
=> Illuminate\Support\Collection {#3205
all: [
1,
],
}
>>> collect(array($a,$a,$a,$a,$a,$a,$a))
=> Illuminate\Support\Collection {#3218
all: [
Illuminate\Support\Collection {#3205
all: [
1,
],
},
Illuminate\Support\Collection {#3205},
Illuminate\Support\Collection {#3205},
Illuminate\Support\Collection {#3205},
Illuminate\Support\Collection {#3205},
Illuminate\Support\Collection {#3205},
Illuminate\Support\Collection {#3205},
],
}
Now, to get only an array of those elements, I use the flatten() method.
>>> collect(array($a,$a,$a,$a,$a,$a,$a))->flatten()
=> Illuminate\Support\Collection {#3220
all: [
1,
1,
1,
1,
1,
1,
1,
],
}
The flatten method accepts an optional depth paramether. Read more about it in the documentation

How tou get values from an array in Laravel

I'm connecting to a 3rd party API can dd the complete result. As a test I'm getting all countries with a name and a type. Now I would like to show just 1 countryname as a test. I'm not sure how to do this.
I tried $countries[0]->name and $countries[0]['name'] but both don't seem to work
Current result:
array:69 [▼
0 => NamedCriterion {#218 ▼
-name: "Andorra"
-type: "Country"
-value: "13"
}
1 => NamedCriterion {#235 ▶}
2 => NamedCriterion {#233 ▶}
3 => NamedCriterion {#220 ▶}
4 => NamedCriterion {#230 ▶}
5 => NamedCriterion {#223 ▶}
6 => NamedCriterion {#221 ▶}
7 => NamedCriterion {#219 ▶}
From this call:
$response = $search->getCriteria([], ['Country'], []);
if (!$response->isError()) {
$criterionSets = $response->getCriterionSets();
$countryCriterionSet = $criterionSets[0];
$countries = $countryCriterionSet->getCriteria();
$resultCount = $response->getResultCount();
}
dd($countries);
}
So I would for example just get Andorra as a result of my call and dd that.

How to combine the output from an object and an array?

I have a db query that returns a value to the $result variable that looks like this:
Collection {#710 ▼
#items: array:16 [▼
0 => {#717 ▼
+"utest_step_id": 18
+"value": "1"
+"count": 26
}
1 => {#716 ▼
+"utest_step_id": 18
+"value": "2"
+"count": 23
}
2 => {#709 ▶}
3 => {#711 ▶}
4 => {#713 ▶}
5 => {#714 ▶}
6 => {#715 ▶}
7 => {#718 ▶}
8 => {#719 ▶}
9 => {#720 ▶}
10 => {#721 ▶}
11 => {#722 ▶}
12 => {#723 ▶}
13 => {#724 ▶}
14 => {#725 ▶}
15 => {#726 ▶}
]
}
I have an array called $steps that I need to combine with the data that looks like this:
[▼
0 => {#676 ▼
+"id": 18
+"step": 1
+"type": "select many image"
+"featured": false
+"data": {#664 ▼
+"description": "Ut quaerat ut sed molestiae."
+"randomize": 0
+"options": array:4 [▼
0 => {#671 ▼
+"position": 1
+"image_url": "http://lorempixel.com/455/255/?67678"
+"caption": "Prof."
+"image_source": "http://lorempixel.com/455/255/?67678"
}
1 => {#668 ▼
+"position": 2
+"image_url": "http://lorempixel.com/455/255/?23876"
+"caption": "Ms."
+"image_source": "http://lorempixel.com/455/255/?23876"
}
2 => {#670 ▼
+"position": 3
+"image_url": "http://lorempixel.com/455/255/?45833"
+"caption": "Prof."
+"image_source": "http://lorempixel.com/455/255/?45833"
}
3 => {#677 ▼
+"position": 4
+"image_url": "http://lorempixel.com/455/255/?83800"
+"caption": "Dr."
+"image_source": "http://lorempixel.com/455/255/?83800"
}
]
}
}
1 => {#690 ▶}
2 => {#697 ▶}
3 => {#704 ▶}
4 => {#706 ▶}
]
I am trying to insert the value from count in $results into the $steps->data->options array.
Here is my code:
foreach ($results as $result) {
$comparison_field = 'position';
for ($i=0; $i < sizeof($steps); $i++) {
if ($result->utest_step_id == $steps[$i]->id) {
foreach ($steps[$i]->data->options as $option) {
$option->count = 0;
$option->count = $result->count;
}
}
}
}
The problem is I am ending up with the count from the last $result->utest_step_id in each count item in the $option.
Where am I going wrong?
You have a for loop iterating over all of the elements in $steps for each entry in $results. This results in everything in $steps being overwritten for each element in $results. If these arrays are supposed to line up, grab an $id from the $results loop:
foreach ($results AS $id=>$result)
and use that instead of $i in a for loop.

How to count the quantity of iten in every group on eloquent

I'm trying to group some records entries based on their statuses, however I just do not want the entries, but rather how many entries I have by status
This is my query :
CallItem::whereBetween('scheduled_date', [$startDate, $endDate])->get()->groupBy('status')->toArray()
That are the result:
array:4 [▼
"swapping" => array:4 [▼
0 => array:24 [▶]
1 => array:24 [▶]
2 => array:24 [▶]
3 => array:24 [▶]
]
"confirmed" => array:3 [▼
0 => array:24 [▶]
1 => array:24 [▶]
2 => array:24 [▶]
]
"canceled" => array:6 [▼
0 => array:24 [▶]
1 => array:24 [▶]
2 => array:24 [▶]
3 => array:24 [▶]
4 => array:24 [▶]
5 => array:24 [▶]
]
"scheduled" => array:5 [▼
0 => array:24 [▶]
1 => array:24 [▶]
2 => array:24 [▶]
3 => array:24 [▶]
4 => array:24 [▶]
]
]
But i want something like this:
array:4[▼
0 => 4,
1 => 3,
2 => 6,
3 => 5
]
only the size of the array in "swapping, confirmed, canceled, scheduled" and with no index.
How can i do this query ?
The map function would be a good choice. Don't call to toArray yet:
$totals = $result->map(function ($item) { return $item->count(); });
All together:
$totals = CallItem::whereBetween('scheduled_date', [$startDate, $endDate])
->get()
->groupBy('status')
->map(function ($group) { return $group->count(); });
Use array_flatten to then remove the indexes.
If you want to count using the database, you can use DB::raw.
$count = \DB::table('call_items')
->select(\DB::raw('count(status) as count'), 'status')
->groupBy('status')
->whereBetween('scheduled_date', [$startDate, $endDate])
->get();
This will return several objects with the status and a count per status.

PHP Get a specific property from an array of objects with different array count

I am in a confusing situation right now.
So, I have an array of objects
array:3 [▼
0 => array:5 [▶]
1 => array:2 [▶]
2 => array:10 [▶]
]
Each array items contains another array which will have objects
array:3 [▼
0 => array:5 [▼
0 => {#215 ▼
+"DefaultTimeLength": 40
+"ProgramID": 4
+"NumDeducted": 1
+"ID": 245
+"Name": "30-Swedish-Massage"
}
1 => {#216 ▼
+"DefaultTimeLength": 70
+"ProgramID": 4
+"NumDeducted": 1
+"ID": 246
+"Name": "60-Swedish-Massage"
}
2 => {#217 ▶}
3 => {#218 ▶}
4 => {#219 ▶}
]
1 => array:2 [▶]
2 => array:10 [▶]
]
What I want to achieve is, I want to get the 'ID' and 'Name' as an array for every array of objects from this array. Since, every array inside the main array have different counts, I cannot use a FOR loop, to get the required data.
Any ideas?
use nested foreach loop e.g:
foreach($main as $m){
foreach($m as $item){
echo $item->ID ." ".$item->Name;
}
}
use 2 foreach loop inside eachother
foreach ($array as $item) {
foreach ($item as $sub) {
echo $sub['ID'] . " " . $sub['Name'] . "<br>";
}
}
your full code will be something like this

Categories