I'm having trouble put values into collection laravel
I've read this with the same problem but didnt worked on me
reference
This is my collection
Illuminate\Support\Collection {#1348
#items: array:9 [
0 => {#1350
+"id": 532
+"id_user": "BR001"
+"name": "Jack"
+"department": "HR
+"City": "London"
+"updated_at": "2021-04-08 15:37:32"
}
1 => {#1351
+"id": 522
+"id_user": "BR002"
+"name": "Rose"
+"department": "IR"
+"city": "London"
+"updated_at": "2021-04-08 15:36:19"
}
2 => {#1352
+"id": 532
+"id_user": "AR023"
+"name": "Michael"
+"department": "RnD"
+"City": "London"
+"updated_at": "2021-04-08 15:45:43"
}
Expected output
Illuminate\Support\Collection {#1348
#items: array:9 [
0 => {#1350
+"id": 532
+"id_user": "BR001"
+"name": "Jack"
+"department": "HR
+"City": "London"
+"updated_at": "2021-04-08 15:37:32"
+"Status": "Single"
}
1 => {#1351
+"id": 522
+"id_user": "BR002"
+"name": "Rose"
+"department": "IR"
+"city": "London"
+"updated_at": "2021-04-08 15:36:19"
+"Status": "Single"
}
2 => {#1352
+"id": 532
+"id_user": "AR023"
+"name": "Michael"
+"department": "RnD"
+"City": "London"
+"updated_at": "2021-04-08 15:45:43"
+"Status": "Single"
}
How to add +"Status": "Single" ?
any help would be appreciated
Thank you
I have assumed your collection name as $collection.
You can either use map directly or create a function using macro,
Using Map Directly
$val = $collection->map(function ($value) {
$value["status"] = "Single";
return $value;
});
dd($val);
Creating custom function using macro
Collection::macro('toAddStatusProperty', function () {
return $this->map(function ($value, $key) {
$value["status"] = "Single";
return $value;
});
});
$val = $collection->toAddStatusProperty();
dd($val);
Related
I'm having 2 multidimensional arrays on php, and I'm trying to get only the subarrays that have matching column value.
1st Array :
0 => {#1 ▼
+"name": "John Doe"
+"education": "Finance"
+"age": "23"
+"mark": "10"
}
1 => {#2 ▼
+"name": "Jane Doe"
+"education": "History"
+"age": "25"
+"mark": "9"
}
2 => {#3 ▼
+"name": "Anne Smith"
+"education": "Computer Science"
+"age": "22"
+"mark": "8"
}
2nd Array :
0 => {#1 ▼
+"name": "Bob Builder"
+"country": "US"
+"city": "Massachusetts"
}
1 => {#2 ▼
+"name": "Jane Doe"
+"country": "France"
+"city": "Paris"
}
2 => {#3 ▼
+"name": "Anne Smith"
+"country": "Germany"
+"city": "Berlin"
}
I want to match on column name.
Desired output should be as follows :
0 => {#2 ▼
+"name": "Jane Doe"
+"country": "France"
+"city": "Paris"
+"education": "History"
+"age": "25"
+"mark": "9"
}
1 => {#3 ▼
+"name": "Anne Smith"
+"country": "Germany"
+"city": "Berlin"
+"education": "Computer Science"
+"age": "22"
+"mark": "8"
}
Any help on this would be appreciated.
Here is one way to do it.
First, reindex the first array by name. This will simplify matching names in the next part.
$first = array_column($first, null, 'name');
Then iterate the second array. If the person exists in the first array, merge the values from the two arrays and append that to the product. If not, don't do anything, since you're only looking for the intersection.
foreach ($second as $person) {
if (isset($first[$person['name']])) {
$product[] = array_merge($first[$person['name']], $person);
}
}
One potential problem with this is that names aren't unique, and this will break if you ever have any people with the same name. It would be much better if you had some unique identifier to use for a person rather than name.
I am trying to convert the array tree to pivot table headers Right Now I have array like this. From this array table headers will be generated dynamically. I have hierarchy through which I want to create dynamic headers of tables:
array:4 [▼
0 => array:1 [▼
0 => {#643 ▼
+"id": 6
+"title": "Col 1"
+"parent": null
+"created_at": "2020-04-27 17:01:08"
+"updated_at": "2020-04-27 17:01:08"
+"children": array:1 [▼
0 => {#639 ▼
+"id": 10
+"title": "Mh Col"
+"parent": "6"
+"created_at": "2020-04-27 17:01:08"
+"updated_at": "2020-04-27 17:01:08"
}
]
}
]
1 => array:1 [▼
0 => {#640 ▼
+"id": 7
+"title": "Col 2"
+"parent": null
+"created_at": "2020-04-27 17:01:08"
+"updated_at": "2020-04-27 17:01:08"
+"children": []
}
]
]
How I am trying to print the table:
#foreach ($tbHeaders as $tb)
<tr>
<th rowspan="2">{{ $tb->title }}</th>
</tr>
#endforeach
I am looking for this type of header:
I cannot update using my javascript or in basic update because the id is the same.. Do you have any idea on how to resolve this? Please help...
How can I solve this, I have this in my controller
$dataReqorder = DB::table('reqorders')
->join('productquantities', 'reqorders.item_id', '=', 'productquantities.id')
->join('products', 'productquantities.prod_id', '=', 'products.id')
->where('req_id', '=', $shoppingId)
->get();
dd($dataReqorder);
And it has a result like this
Collection {#272 ▼
#items: array:3 [▼
0 => {#265 ▼
+"id": 1
+"req_id": "ZJXYGX42RN"
+"item_id": "1"
+"quantity": 100
+"amount": "6600"
+"status": "0"
+"remember_token": null
+"created_at": "2018-03-10 15:18:38"
+"updated_at": "2018-03-11 13:03:15"
+"prod_id": 1
+"brand_id": 1
+"supplier_id": 1
+"branch_id": 3
+"category_id": "1"
+"price": "200"
+"saleprice": "100"
+"priceoption": "regular"
+"description": "brake for yamaha"
+"lenght": "10"
+"width": "10"
+"height": "10"
+"weight": "10"
+"unit": "piece"
+"pic": "1520752307.png"
+"product_name": "Brake"
}
1 => {#286 ▼
+"id": 1
+"req_id": "ZJXYGX42RN"
+"item_id": "2"
+"quantity": 100
+"amount": "67000"
+"status": "0"
+"remember_token": null
+"created_at": "2018-03-10 15:18:38"
+"updated_at": "2018-03-11 13:03:15"
+"prod_id": 1
+"brand_id": 1
+"supplier_id": 1
+"branch_id": 3
+"category_id": "1"
+"price": "200"
+"saleprice": "100"
+"priceoption": "regular"
+"description": "brake for yamaha"
+"lenght": "10"
+"width": "10"
+"height": "10"
+"weight": "10"
+"unit": "piece"
+"pic": "1520752614.png"
+"product_name": "Brake"
}
2 => {#289 ▼
+"id": 1
+"req_id": "ZJXYGX42RN"
+"item_id": "4"
+"quantity": 33
+"amount": "1000"
+"status": "0"
+"remember_token": null
+"created_at": "2018-03-10 15:18:38"
+"updated_at": "2018-03-11 13:03:15"
+"prod_id": 1
+"brand_id": 1
+"supplier_id": 1
+"branch_id": 3
+"category_id": "1"
+"price": "2"
+"saleprice": "2"
+"priceoption": "regular"
+"description": "brake for yamaha"
+"lenght": "12"
+"width": "2"
+"height": "2"
+"weight": "2"
+"unit": "roll"
+"pic": "1520752847.png"
+"product_name": "Brake"
}
]
}
However, I cannot update using my javascript or in basic update because the id is the same.. Do you have any idea on how to resolve this? Please help...
Your query selects all columns in all joined tables.
If multiple columns have the same name (e.g. id), the last one overwrites the previous ones.
You have to limit the selected columns and/or use aliases:
->get(['reqorders.*', 'products.id as products_id']);
I have this laravel collection called $articles_rows.
Collection {#320 ▼
#items: array:3 [▼
0 => array:6 [▼
"img_alt" => "<span>Original</span> gifts"
"class" => "personalised-gifts"
"elements" => array:2 [▼
0 => array:2 [▼
"id" => 2638
"type" => "ARTICLE"
]
1 => array:2 [▼
"id" => 2100
"type" => "ARTICLE"
]
]
]
1 => array:5 [▼
"img_alt" => "<span>Love</span> gifts"
"class" => "love-gifts"
"elements" => array:3 [▼
0 => array:2 [▼
"id" => 1072
"type" => "CATEGORY"
]
1 => array:2 [▼
"id" => 6186
"type" => "ARTICLE"
]
2 => array:2 [▼
"id" => 1028
"type" => "CATEGORY"
]
]
]
On the other hand, I have another collection called $cats_and_arts, this $cat_and_arts collection have extra information about the 'element' field in the $articles_rows collection.
This is my $cats_and_arts collection:
array:5 [▼
0 => {#313 ▼
+"type": "ARTICLE"
+"name": "Ceramic Mug"
+"resource_id": "2638"
+"seo": {#314 ▶}
}
1 => {#323 ▼
+"type": "CATEGORY"
+"name": "Personalised Blankets"
+"category": {#325 ▼
+"id": 1072
+"gallery_id": null
+"final": true
}
+"seo": {#326 ▶}
}
2 => {#327 ▼
+"type": "ARTICLE"
+"name": "Circle Cushion"
+"resource_id": "2100"
+"seo": {#328 ▶}
}
3 => {#329 ▼
+"type": "ARTICLE"
+"name": "Book"
+"resource_id": "6186"
+"seo": {#330 ▶}
}
4 => {#341 ▼
+"type": "CATEGORY"
+"name": "Gifts for men"
+"category": {#342 ▼
+"id": 1028
+"gallery_id": null
+"final": false
}
+"seo": {#343 ▶}
}
]
I want replace the $cats_and_arts CATEGORIES and ARTICLES by the $articles_rows elements field.
I want this final result:
Collection {#320 ▼
#items: array:3 [▼
0 => array:6 [▼
"img_alt" => "<span>Original</span> gifts"
"class" => "personalised-gifts"
"elements" => array:2 [▼
0 => {#313 ▼
+"type": "ARTICLE"
+"name": "Ceramic Mug"
+"resource_id": "2638"
+"seo": {#314 ▶}
}
2 => {#327 ▼
+"type": "ARTICLE"
+"name": "Circle Cushion"
+"resource_id": "2100"
+"seo": {#328 ▶}
}
]
]
1 => array:5 [▼
"img_alt" => "<span>Love</span> gifts"
"class" => "love-gifts"
"elements" => array:3 [▼
0 => {#323 ▼
+"type": "CATEGORY"
+"name": "Personalised Blankets"
+"category": {#325 ▼
+"id": 1072
+"gallery_id": null
+"final": true
}
+"seo": {#326 ▶}
}
1 => {#329 ▼
+"type": "ARTICLE"
+"name": "Book"
+"resource_id": "6186"
+"seo": {#330 ▶}
}
2 => {#341 ▼
+"type": "CATEGORY"
+"name": "Gifts for men"
+"category": {#342 ▼
+"id": 1028
+"gallery_id": null
+"final": false
}
+"seo": {#343 ▶}
}
]
]
And this is my code, but, return me an error
Undefined index:id in the line 'if($article_element['id'] ==
$extra_element->category->id)'
when the loop have more than one iteration:
foreach($articles_rows as &$article)
{
foreach($article['elements'] as &$article_element)
{
foreach($cats_and_arts as $extra_element)
{
if($extra_element->type == 'CATEGORY')
{
if($article_element['id'] == $extra_element->category->id)//Undefined index:id
{
$article_element = (array)$extra_element;
}
}
if($extra_element->type == 'ARTICLE')
{
if($article_element['id'] == $extra_element->article->id)
{
$article_element = (array)$extra_element;
}
}
}
}
}
When you reassign the value of $extra_element, you're "removing" the property id, which you are using in the if.
There are 2 solutions:
Keep the id in your object.
$article_element = array_merge((array)$extra_element, $article_element);
Modify your condition to make sure the property exists in your array.
if (array_key_exists('id', $article_element) && $article_element['id'] == $extra_element->category->id)
Your issue is coming from the use of an array reference iterator in:
foreach($article['elements'] as &$article_element) {...}
Because you later overwrite the entire array at $article_element, the foreach iteration cannot continue. This can probably be corrected by iterating with both a key and value and overwriting the array not by its iterator variable, but rather directly by its key in the parent array.
// Iterate the loop with a variable for the key
// instead of using a reference
foreach($article['elements'] as $key => $article_element)
{
foreach($cats_and_arts as $extra_element)
{
if($extra_element->type == 'CATEGORY')
{
if($article_element['id'] == $extra_element->category->id)//Undefined index:id
{
// Overwrite it by key in the parent array
$article['elements'][$key] = (array)$extra_element;
}
}
if($extra_element->type == 'ARTICLE')
{
if($article_element['id'] == $extra_element->article->id)
{
// And do the same here...
$article['elements'][$key] = (array)$extra_element;
}
}
}
}
Note: I didn't include the outermost loop here. It does not need modification as far as I can tell, although the array reference &$article is probably unnecessary. You may be able to use
foreach ($article_rows as $article) {...}
because I don't see anywhere that you modify that outer iterator.
I have this laravel collection called $box_items.
Collection {#320 ▼
#items: array:3 [▼
0 => array:6 [▼
"img_alt" => "<span>Original</span> gifts"
"class" => "personalised-gifts"
"elements" => array:2 [▼
0 => array:2 [▼
"id" => 2638
"type" => "ARTICLE"
]
1 => array:2 [▼
"id" => 2100
"type" => "ARTICLE"
]
]
]
1 => array:5 [▼
"img_alt" => "<span>Love</span> gifts"
"class" => "love-gifts"
"elements" => array:3 [▼
0 => array:2 [▼
"id" => 1072
"type" => "CATEGORY"
]
1 => array:2 [▼
"id" => 6186
"type" => "ARTICLE"
]
2 => array:2 [▼
"id" => 1028
"type" => "CATEGORY"
]
]
]
On the other hand, I have another collection called $elements, this $elements collection have extra information about the 'element' field in the $box_items collection.
This is my $elements collection:
array:5 [▼
0 => {#313 ▼
+"type": "ARTICLE"
+"name": "Ceramic Mug"
+"resource_id": "2638"
+"seo": {#314 ▶}
}
1 => {#323 ▼
+"type": "CATEGORY"
+"name": "Personalised Blankets"
+"category": {#325 ▼
+"id": 1072
+"gallery_id": null
+"final": true
}
+"seo": {#326 ▶}
}
2 => {#327 ▼
+"type": "ARTICLE"
+"name": "Circle Cushion"
+"resource_id": "2100"
+"seo": {#328 ▶}
}
3 => {#329 ▼
+"type": "ARTICLE"
+"name": "Book"
+"resource_id": "6186"
+"seo": {#330 ▶}
}
4 => {#341 ▼
+"type": "CATEGORY"
+"name": "Gifts for men"
+"category": {#342 ▼
+"id": 1028
+"gallery_id": null
+"final": false
}
+"seo": {#343 ▶}
}
]
I want replace the $elements CATEGORIES and ARTICLES by the $box_items elements field.
I want this final result:
Collection {#320 ▼
#items: array:3 [▼
0 => array:6 [▼
"img_alt" => "<span>Original</span> gifts"
"class" => "personalised-gifts"
"elements" => array:2 [▼
0 => {#313 ▼
+"type": "ARTICLE"
+"name": "Ceramic Mug"
+"resource_id": "2638"
+"seo": {#314 ▶}
}
2 => {#327 ▼
+"type": "ARTICLE"
+"name": "Circle Cushion"
+"resource_id": "2100"
+"seo": {#328 ▶}
}
]
]
1 => array:5 [▼
"img_alt" => "<span>Love</span> gifts"
"class" => "love-gifts"
"elements" => array:3 [▼
0 => {#323 ▼
+"type": "CATEGORY"
+"name": "Personalised Blankets"
+"category": {#325 ▼
+"id": 1072
+"gallery_id": null
+"final": true
}
+"seo": {#326 ▶}
}
1 => {#329 ▼
+"type": "ARTICLE"
+"name": "Book"
+"resource_id": "6186"
+"seo": {#330 ▶}
}
2 => {#341 ▼
+"type": "CATEGORY"
+"name": "Gifts for men"
+"category": {#342 ▼
+"id": 1028
+"gallery_id": null
+"final": false
}
+"seo": {#343 ▶}
}
]
]
EDIT: error in the line if($article_element['id'] == $extra_element->category->id);
foreach($articles_rows as &$article)
{
foreach($article['elements'] as &$article_element)
{
foreach($cats_and_arts as $extra_element)
{
if($extra_element->type == 'CATEGORY')
{
if($article_element['id'] == $extra_element->category->id)//Undefined index: id
{
$article_element = (array)$extra_element;
}
}
if($extra_element->type == 'ARTICLE')
{
if($article_element['id'] == $extra_element->article->id)
{
$article_element = (array)$extra_element;
}
}
}
}
}
dd($articles_rows);
Try this out. It should work for you.
foreach($box_items as &$box){
foreach($box["elements"] as &$box_element){
foreach($elements as $element){
if($box_element->id == $element->id){
$box_element = $element;
}
}
}
}