Grouping a collection IN LARAVEL - arrays

I have an array called $customerRecords. I want to group the data in this array by the customer's email.
This is the array below
$customerRecords = [
{
"id": 1,
"note": "This is note 1",
"customer": [
{
"id": 1,
"user_id": 34,
"email": "doe#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 2,
"note": "This is note 2",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 3,
"note": "This is a note 3",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
]
This is the expected result I want to achieve so that I can know the group of data that belongs to an email .
{
"doe#mailnator.com": [
{
"id": 1,
"note": "This is note 1",
"customer": [
{
"id": 1,
"user_id": 34,
"email": "doe#mailnator.com",
"phone": "9829484857"
}
]
}
],
"john#mailnator.com": [
{
"id": 2,
"note": "This is note 2",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 3,
"note": "This is a note 3",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
}
]
}
So this is what I have tried but it's not working:
return collect($customerRecords)->groupBy('customer.email')

you are almost done just define customer 0 item then email
return collect($customerRecords)->groupBy('customer.0.email');

This is how I was able to solve it.
$grouped = [];
foreach($customerRecords as $value) {
foreach($value['customer'] as $cust) {
$grouped[$cust['email']][] = $value;
}
}

Related

How to display a column data in a react_table when the column data is array of object?

I am using s react table to to display a table of data
In tags column I want display both the tags present in tags array
of object like this. I did tried some ways but didn't get any
success as of yet. New to tables, so any better way to do this
will be appreciated.
code-sandbox link :
CodeSandBox
[
{
"id": 1,
"first_name": "Torie",
"last_name": "Rustman",
"email": "trustman0#amazon.co.uk",
"date_of_birth": "1979-11-16T23:04:32Z",
"age": 45,
"tags": null,
"phone": "6844103517"
},
{
"id": 2,
"first_name": "Kordula",
"last_name": "Gecks",
"email": "kgecks1#deviantart.com",
"date_of_birth": "1997-08-06T21:07:34Z",
"age": 30,
"tags": null,
"phone": "8429683893"
},
{
"id": 3,
"first_name": "Vikki",
"last_name": "Simoens",
"email": "vsimoens2#ted.com",
"date_of_birth": "2016-04-28T16:59:19Z",
"age": 48,
"tags": [
{ "id": 0, "name": "tag1" },
{ "id": 1, "name": "tag2" }
],
"phone": "8672773997"
},
{
"id": 4,
"first_name": "Burnaby",
"last_name": "Cowern",
"email": "bcowern3#forbes.com",
"date_of_birth": "2017-10-25T08:05:50Z",
"age": 54,
"tags": [
{ "id": 0, "name": "tag3" },
{ "id": 1, "name": "tag4" }
],
"phone": "4257971694"
},
{
"id": 5,
"first_name": "Teddie",
"last_name": "Traice",
"email": "ttraice4#zdnet.com",
"date_of_birth": "2015-04-20T11:45:34Z",
"age": 57,
"tags": [
{ "id": 0, "name": "tag5" },
{ "id": 1, "name": "tag6" }
],
"phone": "3932158370"
},
{
"id": 7,
"first_name": "Shayna",
"last_name": "Dimitresco",
"email": "sdimitresco6#uiuc.edu",
"date_of_birth": "1997-10-28T11:25:07Z",
"age": 21,
"tags": null,
"phone": "1216713219"
}
]
You could define the cell display function when you are defining the columns like you are doing for the date field.
{
Header: "Tags",
Footer: "Tags",
accessor: "tags",
// accessor: "tags[0].name"
Cell: ({ value }) => {
const values = value ? value.map((v) => v.name + ' ') : '';
return values;
}
}
Forked sandbox here

How to write the esql code for a json response from the json request as input?

request->
{
"data":
{
"type": "employee",
"attributes":
[
{
"name": "Sam",
"empId": 1,
"grade": "A"
},
{
"name": "Barsa",
"empId": 2,
"grade": "A"
},
"name": "Ram",
"empId": 3,
"grade": "B"
}
]
}
}
response->##
##
{
"data":
{
"type": "grade",
"employees":
[
{
"name": "Sam",
"empId": 1,
"grade": "A"
},
{
"name": "Barsa",
"empId": 2,
"grade": "A"
},
"name": "Ram",
"empId": 3,
"grade": "B"
}
]
}
}
Chapters Modifying a JSON message and SET statement explain how to do the following:
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
SET OutputRoot = InputRoot;
SET OutputRoot.JSON.Data.data.type = 'grade';
SET OutputRoot.JSON.Data.data.attributes NAME = 'employees';
RETURN TRUE;
END;
NOTE: Your request message is invalid json: The { before "name": "Ram", is missing.

Convert array structure from first array to second

This is my multi dimensional array.
Output should be in array2 structure.
I have tried many ways covert above array in easy but all came up with bulky code and lots of iteration.
Is there any easy way to do so?
I am a new in angular and JavaScript.
var array1=[
{
"id": 1,
"name": "Johny",
"category": [
{
"id": 12,
"name": "GUI",
"status": {
"id": 123,
"status": "working",
"name": "GUI"
}
},
{
"id": 13,
"name": "GFX",
"status": {
"id": 124,
"status": "working",
"name": "GFX"
}
},
{
"id": 14,
"name": "UI",
"status": {
"id": 125,
"status": "working",
"name": "UI"
}
}
]
},
{
"id": 2,
"name": "Paul",
"category": [
{
"id": 21,
"name": "GUI",
"status": {
"id": 212,
"status": "Progress",
"name": "GUI"
}
},
{
"id": 22,
"name": "GFX",
"status": {
"id": 221,
"status": "working",
"name": "GFX"
}
},
{
"id": 23,
"name": "DM",
"status": {
"id": 231,
"status": "done",
"name": "DM"
}
}
]
}
]
I want to change into this array2.
var array2=[
{
"id": 1,
"name": "Johny",
"category": [
{
"id": 12,
"name": "GUI",
"data": [
{
"id": 12,
"name": "GUI",
"status": {
"id": 123,
"status": "working",
"name": "GUI"
}
},
{
"id": 21,
"name": "GUI",
"status": {
"id": 212,
"status": "Progress",
"name": "GUI"
}
}
]
},
{
"id": 13,
"name": "GFX",
"data": [
{
"id": 13,
"name": "GFX",
"status": {
"id": 124,
"status": "working",
"name": "GFX"
}
},
{
"id": 22,
"name": "GFX",
"status": {
"id": 221,
"status": "working",
"name": "GFX"
}
}
]
},
{
"id": 14,
"name": "UI",
"data": [
{
"id": 14,
"name": "UI",
"status": {
"id": 125,
"status": "working",
"name": "UI"
}
},
//null data if not in first
{}
]
},
{
"id": 23,
"name": "DM",
"data": [
//null data if not in first
{},
{
"id": 23,
"name": "DM",
"status": {
"id": 231,
"status": "done",
"name": "DM"
}
}
]
}
]
},
{
"id": 2,
"name": "Paul",
"category": [
{
"id": 21,
"name": "GUI",
"data": [
{
"id": 12,
"name": "GUI",
"status": {
"id": 123,
"status": "working",
"name": "GUI"
}
},
{
"id": 21,
"name": "GUI",
"status": {
"id": 212,
"status": "Progress",
"name": "GUI"
}
}
]
},
{
"id": 22,
"name": "GFX",
"data": [
{
"id": 13,
"name": "GFX",
"status": {
"id": 124,
"status": "working",
"name": "GFX"
}
},
{
"id": 22,
"name": "GFX",
"status": {
"id": 221,
"status": "working",
"name": "GFX"
}
}
]
},
{
"id": 14,
"name": "UI",
"data": [
{
"id": 14,
"name": "UI",
"status": {
"id": 125,
"status": "working",
"name": "UI"
}
},
//null data if not in first
{}
]
},
{
"id": 23,
"name": "DM",
"data": [
//null data if not in first
{},
{
"id": 23,
"name": "DM",
"status": {
"id": 231,
"status": "done",
"name": "DM"
}
}
]
}
]
}
]
Output should be in array2 structure.
Thanks in advance.
So you just want to have the original data as additional data attribute? This is simple using a forEach() loop in combination with the map() function:
array1.forEach((obj) => {
obj.category = obj.category.map((cat) => {
return {id: cat.id, name: cat.name, data: cat}
});
})
Hope this is what you're looking for :-)

I can't convert object to an array

I am trying to make a shopping cart API route and I am stuck with this problem because when I am using this statement $carts = \Cart::session($session)->getContent();
I get an object like this:
"cart": {
"1": {
"id": 1,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": "1",
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/AkiGKJdxjKNtyoVI034RPL1drLsMntUxLfzqZplV.jpeg"
},
"conditions": []
},
"2": {
"id": 2,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": 2,
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/IkAhenLttHWaRD58hNZ460ykWCq7q1sih3vI9H5V.jpeg"
},
"conditions": []
}
}
and I want to convet this to be array of objects. I tryied $cart->toArray(); and didn't work, also I tiyed (array) $cart and I get this:
"cart": {
"\u0000*\u0000items": {
"1": {
"id": 1,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": "1",
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/AkiGKJdxjKNtyoVI034RPL1drLsMntUxLfzqZplV.jpeg"
},
"conditions": []
},
"2": {
"id": 2,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": 2,
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/IkAhenLttHWaRD58hNZ460ykWCq7q1sih3vI9H5V.jpeg"
},
"conditions": []
}
}
}
may I know what is the problem?
You can do it manuel like this:
$carts = \Cart::session($session)->getContent();
if ($carts) {
$cartsArray = json_decode($carts);
foreach($cartsArray->cart as &$cart){
$cart = (array) $cart;
}
}
dd($cartsArray);

Array included in array search with elasticsearch

I have users indexed with categories as follows
{
id: 1
name: John
categories: [
{
id: 1
name: Category 1
},
{
id: 2
name: Category 2
}
]
},
{
id: 2
name: Mark
categories: [
{
id: 1
name: Category 1
}
]
}
And I'm trying to get all the documents with Category 1 or Category 2 with
{
filter:
{
bool: {
must: [
{
terms: {user.categories.id: [1, 2]}
}
]
}
}
}
But It only returns the first document that has the two categories, what I am doing wrong?
As I understood, terms search that one of the values is contained in the field, so for user 1
user.categories.id: [1, 2]
user 2
user.categories.id: [1]
Categoy id 1 is contained in both documents
The best way to handle this is probably with a nested filter. You'll have to specify the "nested" type in your mapping, though.
I can set up an index like this:
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"doc": {
"properties": {
"categories": {
"type": "nested",
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "string"
}
}
},
"id": {
"type": "long"
},
"name": {
"type": "string"
}
}
}
}
}
then add some docs:
PUT /test_index/doc/1
{
"id": 1,
"name": "John",
"categories": [
{ "id": 1, "name": "Category 1" },
{ "id": 2, "name": "Category 2" }
]
}
PUT /test_index/doc/2
{
"id": 2,
"name": "Mark",
"categories": [
{ "id": 1, "name": "Category 1" }
]
}
PUT /test_index/doc/3
{
"id": 3,
"name": "Bill",
"categories": [
{ "id": 3, "name": "Category 3" },
{ "id": 4, "name": "Category 4" }
]
}
Now I can use a nested terms filter like this:
POST /test_index/doc/_search
{
"query": {
"constant_score": {
"filter": {
"nested": {
"path": "categories",
"filter": {
"terms": {
"categories.id": [1, 2]
}
}
}
},
"boost": 1.2
}
}
}
...
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"name": "John",
"categories": [
{
"id": 1,
"name": "Category 1"
},
{
"id": 2,
"name": "Category 2"
}
]
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 1,
"_source": {
"id": 2,
"name": "Mark",
"categories": [
{
"id": 1,
"name": "Category 1"
}
]
}
}
]
}
}
Here is the code I used:
http://sense.qbox.io/gist/668aefe910643b52a3a10d40aca67104491668fc

Resources