Here I have this sample data which returns based on category products and I need to limit repeated values.
Raw JSON
[{
"brand": {
"id": "fe877b45-8620-453a-8805-63f0cbd80752",
"name": "No Brand",
"slug": "no-brand",
"description": "null"
},
"options": [{
"id": "324af955-1aa9-42ea-be6e-bb4e5623a97a",
"parent_id": "null",
"name": "Need Insurance?"
},
{
"id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
"parent_id": "null",
"name": "Color",
"slug": "color"
}
],
"rating": [],
"tags": [{
"id": "8a31ee4c-3302-4357-9686-bd4308bbf39f",
"name": "options",
"slug": "options",
"photo": "null"
}],
"variations": [{
"id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3",
"parent_id": "null",
"name": "Ram",
"slug": "ram",
"photo": "null"
},
{
"id": "e5c70766-a558-4539-b41f-77f72c819a7c",
"parent_id": "null",
"name": "cpu",
"slug": "cpu",
"photo": "null"
},
{
"id": "e63ac831-f595-4889-83d2-a5be65734758",
"parent_id": "null",
"name": "Monitor",
"slug": "monitor"
}
]
},
{
"brand": {
"id": "fe877b45-8620-453a-8805-63f0cbd80752",
"name": "No Brand",
"slug": "no-brand",
"description": null
},
"options": [{
"id": "522da418-eb3f-43e9-9392-63c941842a52",
"parent_id": null,
"name": "Color",
"slug": "color-3"
}],
"rating": [],
"tags": [],
"variations": [{
"id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea",
"parent_id": null,
"name": "cpu",
"slug": "cpu-3",
"photo": null
}]
}
]
Sample data
filters: [{,…}, {,…}]
0: {,…}
brand: {id: "fe877b45-8620-453a-8805-63f0cbd80752", name: "No Brand", slug: "no-brand", description: null,…}
options: [{id: "324af955-1aa9-42ea-be6e-bb4e5623a97a", parent_id: null, name: "Need Insurance?",…},…]
0: {id: "324af955-1aa9-42ea-be6e-bb4e5623a97a", parent_id: null, name: "Need Insurance?",…}
1: {id: "73298c18-4ccc-4138-afa5-71d3d00dff9b", parent_id: null, name: "Color", slug: "color",…}
rating: []
tags: [{id: "8a31ee4c-3302-4357-9686-bd4308bbf39f", name: "options", slug: "options", photo: null,…},…]
variations: [,…]
0: {id: "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3", parent_id: null, name: "Ram", slug: "ram", photo: null,…}
1: {id: "e5c70766-a558-4539-b41f-77f72c819a7c", parent_id: null, name: "cpu", slug: "cpu", photo: null,…}
2: {id: "e63ac831-f595-4889-83d2-a5be65734758", parent_id: null, name: "Monitor", slug: "monitor",…}
1: {,…}
brand: {id: "fe877b45-8620-453a-8805-63f0cbd80752", name: "No Brand", slug: "no-brand", description: null,…}
options: [{id: "522da418-eb3f-43e9-9392-63c941842a52", parent_id: null, name: "Color", slug: "color-3",…}]
0: {id: "522da418-eb3f-43e9-9392-63c941842a52", parent_id: null, name: "Color", slug: "color-3",…}
rating: []
tags: [,…]
variations: [,…]
0: {id: "8e9a26c5-2ee4-4d86-9244-a10596d67fea", parent_id: null, name: "cpu", slug: "cpu-3", photo: null,…}
Explanation
As you can see each of my products has same fields of data and some of them are the same, for instance in options both products have Color or in variations both have cpu or brand of both is the same, in final results I need to have only 1 Color and 1 cpu and 1 brand as they are the same.
code
This is how code above returns
$data = [];
foreach($products as $i => $product) {
$data[$i]['brand'] = $product->brand;
$data[$i]['rating'] = $product->rating;
$data[$i]['variations'] = $product->variations;
$data[$i]['options'] = $product->options;
$data[$i]['tags'] = $product->tags;
}
Note: the final result is a merge of all products data into single array, but unique values. That's what I'm looking for.
Any idea?
Update
sample of final result would be something like this
finalResult: [{,…}, {,…}]
0: {,…}
brand: {id: "fe877b45-8620-453a-8805-63f0cbd80752", name: "No Brand", slug: "no-brand", description: null,…}
0: {id: "fe877b45-8620-453a-8805-63f0cbd80752", name: "No Brand", slug: "no-brand", description: null,…},…] // it was same in both products
options: [{id: "324af955-1aa9-42ea-be6e-bb4e5623a97a", parent_id: null, name: "Need Insurance?",…},…]
0: {id: "324af955-1aa9-42ea-be6e-bb4e5623a97a", parent_id: null, name: "Need Insurance?",…}
1: {id: "73298c18-4ccc-4138-afa5-71d3d00dff9b", parent_id: null, name: "Color", slug: "color",…} // it was same in both products
rating: []
tags: [{id: "8a31ee4c-3302-4357-9686-bd4308bbf39f", name: "options", slug: "options", photo: null,…},…]
0: {id: "8a31ee4c-3302-4357-9686-bd4308bbf39f", name: "options", slug: "options", photo: null,…}
1: {id: "94ef99b6-ed2a-4eea-9248-e4775159eb58", name: "product", slug: "product", photo: null,…}
2: {id: "378802b3-d13a-48c4-afa2-f9fed94d69ee", name: "werg", slug: "werg", photo: null, active: "yes",…} // added from another product
3: {id: "f1380f50-af59-4f6a-8eca-d40689c1c1c1", name: "werwg", slug: "werwg", photo: null, active: "yes",…} // added from another product
variations: [,…]
0: {id: "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3", parent_id: null, name: "Ram", slug: "ram", photo: null,…}
1: {id: "e5c70766-a558-4539-b41f-77f72c819a7c", parent_id: null, name: "cpu", slug: "cpu", photo: null,…} // it was same in both products
2: {id: "e63ac831-f595-4889-83d2-a5be65734758", parent_id: null, name: "Monitor", slug: "monitor",…}
Update 2
Getting children of data
Options and Variations both have children (i.e Cpu => [Core i7, Core i3]) or color => ['red', 'black'] so I need to collect this children's and put them under their parents data.
Option model
public function options()
{
return $this->hasMany(Option::class);
}
public function children() {
return $this->hasMany(Option::class,'parent_id','id') ;
}
public function parent()
{
return $this->belongsTo(Option::class,'parent_id');
}
public function isParent()
{
return !$this->parent_id ? true : false; // if parent_id is null => is a Parent Option
}
Variant model
public function variants()
{
return $this->hasMany(Variant::class);
}
public function children() {
return $this->hasMany(Variant::class,'parent_id','id') ;
}
public function parent()
{
return $this->belongsTo(Variant::class,'parent_id');
}
public function isParent()
{
return !$this->parent_id ? true : false; // if parent_id is null => is a Parent Variant
}
Note: as you probably understand relationship between parent and children are defined by parent_id column.
Sample data (includes all arrays and their childs)
{
"brands": [
{
"id": "fe877b45-8620-453a-8805-63f0cbd80752",
"name": "no brand",
"slug": "no-brand",
"description": null,
"photo": null,
"created_at": "2020-07-15 11:35:18",
"updated_at": "2020-07-15 11:35:18"
}
],
"options": [
{
"id": "324af955-1aa9-42ea-be6e-bb4e5623a97a",
"parent_id": null,
"name": "need insurance?",
"slug": "need-insurance",
"photo": null,
"type": "radio",
"active": "yes",
"created_at": "2020-07-17 11:28:09",
"updated_at": "2020-07-17 11:28:09",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"option_id": "324af955-1aa9-42ea-be6e-bb4e5623a97a"
},
"children": [
{
"id": "44afca9e-abf1-4a7a-9c46-d96d8127c2af",
"parent_id": "324af955-1aa9-42ea-be6e-bb4e5623a97a",
"name": "No",
"slug": "no",
"photo": null,
"type": "radio",
"active": "yes",
"created_at": "2020-07-17 11:28:09",
"updated_at": "2020-07-17 11:28:09"
}
]
},
{
"id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
"parent_id": null,
"name": "color",
"slug": "color",
"photo": null,
"type": "dropdown",
"active": "yes",
"created_at": "2020-07-17 11:27:41",
"updated_at": "2020-07-17 11:27:41",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"option_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b"
},
"children": [
{
"id": "29b62f35-52a2-4a8b-ac8f-7e70e065488a",
"parent_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
"name": "Black",
"slug": "black",
"photo": null,
"type": "dropdown",
"active": "yes",
"created_at": "2020-07-17 11:27:41",
"updated_at": "2020-07-17 11:27:41"
},
{
"id": "4aa2d899-f1cc-4000-95e6-997d28dc51fc",
"parent_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
"name": "Red",
"slug": "red",
"photo": null,
"type": "dropdown",
"active": "yes",
"created_at": "2020-07-17 11:27:41",
"updated_at": "2020-07-17 11:27:41"
},
{
"id": "5f9de5bc-e966-48f3-b78c-de709dba86b5",
"parent_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
"name": "Gray",
"slug": "gray",
"photo": null,
"type": "dropdown",
"active": "yes",
"created_at": "2020-07-17 11:27:41",
"updated_at": "2020-07-17 11:27:41"
},
{
"id": "f248d6ab-1b88-4ea4-8d6a-62fe271bfa8a",
"parent_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
"name": "White",
"slug": "white",
"photo": null,
"type": "dropdown",
"active": "yes",
"created_at": "2020-07-17 11:27:41",
"updated_at": "2020-07-17 11:27:41"
}
]
},
{
"id": "522da418-eb3f-43e9-9392-63c941842a52",
"parent_id": null,
"name": "color",
"slug": "color-3",
"photo": null,
"type": "radio",
"active": "yes",
"created_at": "2020-07-17 12:20:46",
"updated_at": "2020-07-17 12:20:46",
"pivot": {
"product_id": "a8bb27c8-e968-4317-b4d2-8e5cd6049ff8",
"option_id": "522da418-eb3f-43e9-9392-63c941842a52"
},
"children": [
{
"id": "84135f25-690b-407b-8c98-e7526429a594",
"parent_id": "522da418-eb3f-43e9-9392-63c941842a52",
"name": "Red",
"slug": "red-3",
"photo": null,
"type": "radio",
"active": "yes",
"created_at": "2020-07-17 12:20:46",
"updated_at": "2020-07-17 12:20:46"
},
{
"id": "9d1f0d9c-272a-4e96-ac0a-aeac869bfc30",
"parent_id": "522da418-eb3f-43e9-9392-63c941842a52",
"name": "Yellow",
"slug": "yellow-2",
"photo": null,
"type": "radio",
"active": "yes",
"created_at": "2020-07-17 12:20:46",
"updated_at": "2020-07-17 12:20:46"
}
]
}
],
"ratings": [
"4.5",
"4.0"
],
"tags": [
{
"id": "8a31ee4c-3302-4357-9686-bd4308bbf39f",
"name": "options",
"slug": "options",
"photo": null,
"active": "yes",
"created_at": "2020-07-17 11:29:47",
"updated_at": "2020-07-17 11:29:47",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"tag_id": "8a31ee4c-3302-4357-9686-bd4308bbf39f"
}
},
{
"id": "94ef99b6-ed2a-4eea-9248-e4775159eb58",
"name": "product",
"slug": "product",
"photo": null,
"active": "yes",
"created_at": "2020-07-17 11:29:47",
"updated_at": "2020-07-17 11:29:47",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"tag_id": "94ef99b6-ed2a-4eea-9248-e4775159eb58"
}
},
{
"id": "378802b3-d13a-48c4-afa2-f9fed94d69ee",
"name": "werg",
"slug": "werg",
"photo": null,
"active": "yes",
"created_at": "2020-07-15 11:53:13",
"updated_at": "2020-07-15 11:53:13",
"pivot": {
"product_id": "a8bb27c8-e968-4317-b4d2-8e5cd6049ff8",
"tag_id": "378802b3-d13a-48c4-afa2-f9fed94d69ee"
}
},
{
"id": "f1380f50-af59-4f6a-8eca-d40689c1c1c1",
"name": "werwg",
"slug": "werwg",
"photo": null,
"active": "yes",
"created_at": "2020-07-15 11:53:13",
"updated_at": "2020-07-15 11:53:13",
"pivot": {
"product_id": "a8bb27c8-e968-4317-b4d2-8e5cd6049ff8",
"tag_id": "f1380f50-af59-4f6a-8eca-d40689c1c1c1"
}
}
],
"variations": [
{
"id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3",
"parent_id": null,
"name": "ram",
"slug": "ram",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 11:27:05",
"updated_at": "2020-07-17 11:27:05",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"variant_id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3"
},
"children": [
{
"id": "5687d6a8-12df-41b2-bf2f-b822faae8af0",
"parent_id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3",
"name": "4 Gig",
"slug": "4 Gig",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 11:27:05",
"updated_at": "2020-07-17 11:27:05"
}
]
},
{
"id": "e5c70766-a558-4539-b41f-77f72c819a7c",
"parent_id": null,
"name": "cpu",
"slug": "cpu",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 11:26:58",
"updated_at": "2020-07-17 11:26:58",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"variant_id": "e5c70766-a558-4539-b41f-77f72c819a7c"
},
"children": [
{
"id": "83003a24-cc69-4305-8d3a-e99da91d3354",
"parent_id": "e5c70766-a558-4539-b41f-77f72c819a7c",
"name": "Core i7",
"slug": "Core i7",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 11:26:58",
"updated_at": "2020-07-17 11:26:58"
}
]
},
{
"id": "e63ac831-f595-4889-83d2-a5be65734758",
"parent_id": null,
"name": "monitor",
"slug": "monitor",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 11:27:21",
"updated_at": "2020-07-17 11:27:21",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"variant_id": "e63ac831-f595-4889-83d2-a5be65734758"
},
"children": [
{
"id": "816e1fab-24eb-49e9-9b3a-d4b4cce16cdf",
"parent_id": "e63ac831-f595-4889-83d2-a5be65734758",
"name": "14\"",
"slug": "14\"",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 11:27:21",
"updated_at": "2020-07-17 11:27:21"
}
]
},
{
"id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea",
"parent_id": null,
"name": "cpu",
"slug": "cpu-3",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 12:20:56",
"updated_at": "2020-07-17 12:20:56",
"pivot": {
"product_id": "a8bb27c8-e968-4317-b4d2-8e5cd6049ff8",
"variant_id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea"
},
"children": [
{
"id": "50857808-106e-4ae0-8c02-a54761e6dac7",
"parent_id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea",
"name": "Core i3",
"slug": "Core i3-2",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 12:20:56",
"updated_at": "2020-07-17 12:20:56"
}
]
}
]
}
You can achieve what you want in one iteration over the data using reduce like so:
$variations = [];
$result = array_reduce($filters, function ($result, $filter) use ($variations) {
$filter['brand']['name'] = strtolower($filter['brand']['name']);
if ($result['brands']->where('name', $filter['brand']['name'])->isEmpty()) {
$result['brands']->push($filter['brand']);
}
foreach ($filter['options'] as $option) {
$option['name'] = strtolower($option['name']);
if ($result['options']->where('name', $option['name'])->isEmpty()) {
$result['options']->push($option);
}
}
if (isset($filter['rating']['id'])) {
if ($result['ratings']->where('id', $filter['rating']['id'])->isEmpty()) {
$result['ratings']->push($filter['rating']);
}
}
foreach ($filter['tags'] as $tag) {
$tag['name'] = strtolower($tag['name']);
if ($result['tags']->where('name', $tag['name'])->isEmpty()) {
$result['tags']->push($tag);
}
}
foreach ($filter['variations'] as $variation) {
$variation['name'] = strtolower($variation['name']);
$variationName = $variation['name'];
$children = collect($variation['children'])->pluck('name');
if ($result['variations']->where('name', $variation['name'])->isEmpty()) {
$result['variations']->push($variation);
$variations[$variationName] = $children;
} else {
$different = $variations[$variationName]->diff($children);
if ($different->isNotEmpty()) {
$result['variations']->push($variation);
foreach ($different as $childName) {
$variations[$variationName]->push($childName);
}
}
}
}
return $result;
}, collect([
'brands' => collect(),
'options' => collect(),
'ratings' => collect(),
'tags' => collect(),
'variations' => collect()
]));
If you need the result as an array, you can use the collection's toArray method:
$result->toArray();
I have added a sample code with a row json data based on your input
$jsonData = '[{
"brand": {"id": "fe877b45-8620-453a-8805-63f0cbd80752", "name": "No Brand", "slug": "no-brand", "description": "null"},
"options": [{"id": "324af955-1aa9-42ea-be6e-bb4e5623a97a", "parent_id": "null", "name": "Need Insurance?"},
{"id": "73298c18-4ccc-4138-afa5-71d3d00dff9b", "parent_id": "null", "name": "Color", "slug": "color"}],
"rating": [],
"tags": [{"id": "8a31ee4c-3302-4357-9686-bd4308bbf39f", "name": "options", "slug": "options", "photo": "null"}],
"variations": [{"id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3", "parent_id": "null", "name": "Ram", "slug": "ram", "photo": "null"},
{"id": "e5c70766-a558-4539-b41f-77f72c819a7c", "parent_id": "null", "name": "cpu", "slug": "cpu", "photo": "null"},
{"id": "e63ac831-f595-4889-83d2-a5be65734758", "parent_id": "null", "name": "Monitor", "slug": "monitor"}
]
},
{
"brand": {"id": "fe877b45-8620-453a-8805-63f0cbd80752", "name": "No Brand", "slug": "no-brand", "description": null},
"options": [{"id": "522da418-eb3f-43e9-9392-63c941842a52", "parent_id": null, "name": "Color", "slug": "color-3"}],
"rating": [],
"tags": [],
"variations": [{"id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea", "parent_id": null, "name": "Cpu", "slug": "cpu-3", "photo": null}]
}
]
';
$jsonDataArr =json_decode($jsonData);
$data = array();
foreach($jsonDataArr as $key => $items){
foreach($items as $innerKey => $eachItem){
if(!isset($data[$innerKey])){
$data[$innerKey] = array();
if(is_array($eachItem)){
foreach($eachItem as $each)
if(!empty($each))
$data[$innerKey][] = (array) $each;
}else{
if(!empty($eachItem))
$data[$innerKey][] = (array) $eachItem;
}
}else{
if(is_array($eachItem)){
foreach($eachItem as $each)
if(!empty($each))
$data[$innerKey][] = (array) $each;
}else{
if(!empty($eachItem))
$data[$innerKey][] = (array) $eachItem;
}
}
}
}
foreach($data as $key => $val){
foreach($val as $l => $item){
$index = trim(strtolower($item['name']));
$data[$key][$index] = $item;
unset($data[$key][$l]);
}
}
foreach($data as $key => $val){
$data[$key]=array_values($val);
}
print_r($data);
Demo
Related
I have a collection that looks like this,
[
{
"id": 1,
"title": "Cooking",
"deleted_at": null,
"created_at": null,
"updated_at": null,
"listings": [
{
"id": 6,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:35:56.000000Z",
"updated_at": "2020-06-03T22:35:56.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 6
},
"assets": [],
"primary_image": []
},
{
"id": 7,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:36:10.000000Z",
"updated_at": "2020-06-03T22:36:10.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 7
},
"assets": [],
"primary_image": []
},
{
"id": 8,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:36:26.000000Z",
"updated_at": "2020-06-03T22:36:26.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 8
},
"assets": [],
"primary_image": []
},
{
"id": 9,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:36:42.000000Z",
"updated_at": "2020-06-03T22:36:42.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 9
},
"assets": [],
"primary_image": []
},
{
"id": 10,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:37:40.000000Z",
"updated_at": "2020-06-03T22:37:40.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 10
},
"assets": [],
"primary_image": []
},
{
"id": 11,
"title": "Listing Number 2",
"slug": "listing-number-2",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:31.000000Z",
"updated_at": "2020-06-03T22:46:31.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 11
},
"assets": [
{
"id": 3,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-lg.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 11,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:33.000000Z",
"updated_at": "2020-06-03T22:46:33.000000Z"
},
{
"id": 4,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-sm.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 11,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:33.000000Z",
"updated_at": "2020-06-03T22:46:33.000000Z"
}
],
"primary_image": [
{
"id": 3,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-lg.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 11,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:33.000000Z",
"updated_at": "2020-06-03T22:46:33.000000Z"
},
{
"id": 4,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-sm.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 11,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:33.000000Z",
"updated_at": "2020-06-03T22:46:33.000000Z"
}
]
},
{
"id": 12,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:50:31.000000Z",
"updated_at": "2020-06-03T22:50:31.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 12
},
"assets": [],
"primary_image": []
},
{
"id": 13,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:57:12.000000Z",
"updated_at": "2020-06-03T22:57:12.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 13
},
"assets": [],
"primary_image": []
},
{
"id": 15,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:00:49.000000Z",
"updated_at": "2020-06-03T23:00:49.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 15
},
"assets": [],
"primary_image": []
},
{
"id": 16,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:01:21.000000Z",
"updated_at": "2020-06-03T23:01:21.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 16
},
"assets": [],
"primary_image": []
},
{
"id": 17,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:01:26.000000Z",
"updated_at": "2020-06-03T23:01:26.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 17
},
"assets": [],
"primary_image": []
},
{
"id": 18,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:01:49.000000Z",
"updated_at": "2020-06-03T23:01:49.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 18
},
"assets": [],
"primary_image": []
},
{
"id": 19,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:02:19.000000Z",
"updated_at": "2020-06-03T23:02:19.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 19
},
"assets": [],
"primary_image": []
},
{
"id": 20,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:03:31.000000Z",
"updated_at": "2020-06-03T23:03:31.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 20
},
"assets": [],
"primary_image": []
},
{
"id": 21,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:25.000000Z",
"updated_at": "2020-06-03T23:06:25.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 21
},
"assets": [
{
"id": 5,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-lg.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 21,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:26.000000Z",
"updated_at": "2020-06-03T23:06:26.000000Z"
},
{
"id": 6,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-sm.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 21,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:27.000000Z",
"updated_at": "2020-06-03T23:06:27.000000Z"
}
],
"primary_image": [
{
"id": 5,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-lg.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 21,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:26.000000Z",
"updated_at": "2020-06-03T23:06:26.000000Z"
},
{
"id": 6,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-sm.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 21,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:27.000000Z",
"updated_at": "2020-06-03T23:06:27.000000Z"
}
]
}
]
}
]
I only want to see a collection that contains items that don't have empty assets array,
I have tried this,
$category = Category::whereHas('listings.assets')->with(['listings.assets', 'listings.primaryImage' => function($query){
$query->where('assets.is_primary', '=', 1);
}])->get();
return $category->filter(function($item){
foreach($item->listings as $i) {
return !empty($i->assets);
}
});
But this just returns an empty [] what am I doing wrong?
You are returning in your loop, so as soon as it finds listings that does not have assets it will break the loop and be false. Instead use contains(), that check if any of the elements, passes the given condition. If one does, it will return true. Therefor not breaking the loop on empty collections, as you do now.
return $category->filter(function($item){
return $item->listings->contains(function ($listing) {
return $listing->assets->isNotEmpty();
});
});
I have resource API that gives me the result but I want to group them based on relationship model.
Code
controller
$outlet = Outlet::where('slug', $slug)->with(['barcodes.product', 'damages', 'images'])->first();
result
{
"data": {
"id": 1,
"name": "Outlet One",
"slug": "outlet-one",
"address": "Jl Raya Bogor No.200",
"cover": null,
"phone": "0211111112",
"products": [
{
"id": 1,
"sku": "AB001SU",
"serial_number": 5245412185,
"price": 120000,
"discount": null,
"product": {
"id": 1,
"name": "Product One",
"slug": "product-one",
"stock": "70",
"cover": null,
"description": "This is first product description.",
"sku": "AB001SU",
"price": 120000,
"discount": null
}
},
{
"id": 2,
"sku": "FD51",
"serial_number": 778516,
"price": 75300,
"discount": 5300,
"product": {
"id": 1,
"name": "Product One",
"slug": "product-one",
"stock": "70",
"cover": null,
"description": "This is first product description.",
"sku": "AB001SU",
"price": 120000,
"discount": null
}
},
{
"id": 3,
"sku": "7609FS",
"serial_number": 232547544,
"price": 35900,
"discount": null,
"product": {
"id": 2,
"name": "Product Two",
"slug": "product-two",
"stock": "120",
"cover": null,
"description": "This is second product description.",
"sku": "FRY8016",
"price": 450000,
"discount": 50000
}
}
]
},
"message": "Outlet retrieved successfully."
}
As you can see in products": [...] id 1 and 2 are belong to Product One is that possible for me to group by my barcodes.product based on product model?
Update
To be more clear; What I'm looking for is something like this:
{
"data": {
"id": 1,
"name": "Outlet One",
"slug": "outlet-one",
"address": "Jl Raya Bogor No.200",
"cover": null,
"phone": "0211111112",
"products": [
{
"id": 1,
"name": "Product One",
"slug": "product-one",
"stock": "70",
"cover": null,
"description": "This is first product description.",
"sku": "AB001SU",
"price": 120000,
"discount": null
"barcodes": { // now barcodes are grouped by prodcuts
"id": 1,
"sku": "AB001SU",
"serial_number": 5245412185,
"price": 120000,
"discount": null,
},
{
"id": 2,
"sku": "FD51",
"serial_number": 778516,
"price": 75300,
"discount": 5300,
},
},
{
"id": 2,
"name": "Product Two",
"slug": "product-two",
"stock": "120",
"cover": null,
"description": "This is second product description.",
"sku": "FRY8016",
"price": 450000,
"discount": 50000
"barcodes": { // now barcodes are grouped by prodcuts
"id": 3,
"sku": "7609FS",
"serial_number": 232547544,
"price": 35900,
"discount": null,
}
}
]
},
"message": "Outlet retrieved successfully."
}
Update 2
Outlet model
public function barcodes()
{
return $this->belongsToMany(Barcode::class, 'outlet_products');
}
Barcode model
public function outlets()
{
return $this->belongsToMany(Outlet::class, 'outlet_products');
}
public function product()
{
return $this->belongsTo(Product::class);
}
Never tried something like this but you maybe could do this in your Outlet model
use App\Http\Resources\ProductsResource;
public function getProductsAttribute()
{
$barcodes = $this->barcodes
->loadMissing('product')
->makeHidden('product');
$products = $barcodes->pluck('product')->keyBy('id');
$groupedBarcodes = $barcodes->groupBy(function ($barcode) {
return $barcode->product->id;
});
return ProductsResource::collection($products->map(function ($product, $id) use ($groupedBarcodes) {
return $product->setAttribute('barcodes', $groupedBarcodes[$id]);
}))->resolve();
}
What is your resource output with that?
In your Outlet Model write this
public function products(){
return $this->hasMany(Product::class, 'product_id','id');
}
In your Product Model write this
public function barcodes(){
return $this->hasMany(Barcode::class,'barcode_id','id');
}
Then for your query just write this
$outlet = Outlet::where('slug', $slug)->products()->barcodes()->with(['your_field'])->first();
I have an api ready that returns the following json response.
Response:
{
"success": true,
"conversation": [{
"id": 37,
"type": "1",
"name": "Sonali",
"created_at": "2019-02-18 13:26:10",
"updated_at": "2019-02-18 20:32:54",
"unread_count": 2,
"chat": {
"id": 357,
"conversation_id": "23",
"type": "text",
"sender_id": "37",
"receiver_id": "39",
"data": "{\"text\":\"hello....\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:10",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Sonali"
}
},
{
"id": 38,
"type": "1",
"name": "Raviraj",
"created_at": "2019-02-18 20:23:55",
"updated_at": "2019-02-18 20:32:47",
"unread_count": 0,
"chat": {
"id": 354,
"conversation_id": "22",
"type": "text",
"sender_id": "39",
"receiver_id": "38",
"data": "{\"text\":\"hey....\"}",
"delivered_at": null,
"seen_at": null,
"created_at": "2019-02-20 13:24:35",
"updated_at": "2019-02-20 13:24:35",
"sender_name": "Nitesh Kesarkar"
}
},
{
"id": 27,
"type": "1",
"name": "Rakesh",
"created_at": "2019-02-01 10:48:19",
"updated_at": "2019-02-07 11:35:10",
"unread_count": 1,
"chat": {
"id": 358,
"conversation_id": "21",
"type": "text",
"sender_id": "27",
"receiver_id": "39",
"data": "{\"text\":\"hello\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:24",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Rakesh Patil"
}
}
]
}
This response consists of list of users and their associated last chat message for currently logged in user. I want to sort this collection according to the latest message first. How can I do that?
Sorting should be based on the chat.created_at field.
Expected Result:
{
"success": true,
"conversation": [
{
"id": 27,
"type": "1",
"name": "Rakesh",
"created_at": "2019-02-01 10:48:19",
"updated_at": "2019-02-07 11:35:10",
"unread_count": 1,
"chat": {
"id": 358,
"conversation_id": "21",
"type": "text",
"sender_id": "27",
"receiver_id": "39",
"data": "{\"text\":\"hello\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:24",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Rakesh Patil"
}
},
{
"id": 37,
"type": "1",
"name": "Sonali",
"created_at": "2019-02-18 13:26:10",
"updated_at": "2019-02-18 20:32:54",
"unread_count": 2,
"chat": {
"id": 357,
"conversation_id": "23",
"type": "text",
"sender_id": "37",
"receiver_id": "39",
"data": "{\"text\":\"hello....\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:10",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Sonali"
}
},
{
"id": 38,
"type": "1",
"name": "Raviraj",
"created_at": "2019-02-18 20:23:55",
"updated_at": "2019-02-18 20:32:47",
"unread_count": 0,
"chat": {
"id": 354,
"conversation_id": "22",
"type": "text",
"sender_id": "39",
"receiver_id": "38",
"data": "{\"text\":\"hey....\"}",
"delivered_at": null,
"seen_at": null,
"created_at": "2019-02-20 13:24:35",
"updated_at": "2019-02-20 13:24:35",
"sender_name": "Nitesh Kesarkar"
}
}
]
}
UPDATE :
Adding these lines worked as expected. Thanks #JCode
$sorted = $chats->sortByDesc('chat.created_at');
$chats = $sorted->values()->all();
You are looking for the sortBy() method.
This will of course work if your JSON output can be turned into a collection via collect()—in a perfect scenario, your chat messages would be a model in Laravel.
I am using php wrapper, try create an item, all is ok, item is creating, but I can't change status, tried different ways, but can't find right way.
Need change status to "Closed" - http://prntscr.com/fsrwc3
Codes not works:
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>13]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
or
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>13]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
$get_item = PodioItem::get_basic($item->item_id);
$get_item->fields['status']->values = ['id'=>13];
$get_item->save();
After create item (after new PodioItem...), if I get fields after this code (just get $item->fields or PodioItem::get_basic...), I can see correct status, only in the code, only immediatly after create item, but if I get this item in the another code (just PodioItem::get_basic...) I will see default value, so code don't change status, looks like I just see some cache.
If I create item on the one script:
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>13]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
Then, in the another script update field, it will be change:
$get_item = PodioItem::get_basic('639637317');
$get_item->fields['status']->values = ['id'=>13];
$get_item->save();
Update - debug information:
Get log by test code -
PodioItem::create($app_id, ['fields' => ['status'=>[13], 'category'=>[3], 'contract-type'=>[4]]]);
(simple variant for creating an item, have the same problems like new PodioItem... )
All fields have category type, but:
status - have inline show type - don't chnages
category - have dropdown show type - Is changing
contract-type - have dropdown show type - Is changing
Log:
2017-07-08 11:07:22 200 POST /item/app/12152727/
2017-07-08 11:07:22 Request body: {"fields":{"status":[8],"category":[3],"contract-type":[4]}}
2017-07-08 11:07:22 Reponse: {
"presence": {
"ref_type": "item",
"ref_id": 641331142,
"user_id": 4194774,
"signature": "c165b85090e6ad28e74ae4baf93ee56113f88bc9"
},
"app": {
"status": "active",
"sharefile_vault_url": null,
"name": "Projects",
"default_view_id": null,
"url_add": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects\/items\/new",
"icon_id": 378,
"link_add": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects\/items\/new",
"app_id": 12152727,
"current_revision": 141,
"item_name": "Project",
"link": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects",
"url": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects",
"url_label": "projects",
"config": {
"item_name": "Project",
"icon_id": 378,
"type": "standard",
"name": "Projects",
"icon": "378.png"
},
"space_id": 3466816,
"icon": "378.png"
},
"created_on": "2017-07-10 15:31:16",
"last_event_on": "2017-07-10 15:31:16",
"linked_account_data": null,
"sharefile_vault_folder_id": null,
"app_item_id_formatted": "3988",
"recurrence": null,
"title": "ACSC",
"participants": {},
"created_by": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"priority": 641331142.0,
"created_via": {
"url": null,
"auth_client_id": 25162,
"display": true,
"name": "importer",
"id": 25162
},
"subscribed_count": 1,
"reminder": null,
"ref": null,
"revision": 0,
"app_item_id": 3988,
"link": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects\/items\/3988",
"item_id": 641331142,
"sharefile_vault_url": null,
"rights": ["subscribe", "grant", "add_conversation", "rate", "update", "delete", "add_file", "grant_view", "view", "comment", "add_task"],
"fields": [{
"status": "active",
"type": "category",
"field_id": 93352415,
"label": "Division",
"values": [{
"value": {
"status": "active",
"text": "ACSC",
"id": 3,
"color": "D2E4EB"
}
}],
"config": {
"default_value": null,
"unique": false,
"description": null,
"hidden_create_view_edit": false,
"required": true,
"mapping": null,
"label": "Division",
"visible": true,
"delta": 5,
"hidden": false,
"settings": {
"multiple": false,
"options": [{
"status": "active",
"text": "ACSE",
"id": 1,
"color": "DCEBD8"
}, {
"status": "active",
"text": "ACSB",
"id": 2,
"color": "F7F0C5"
}, {
"status": "active",
"text": "ACSC",
"id": 3,
"color": "D2E4EB"
}],
"display": "dropdown"
}
},
"external_id": "category"
}, {
"status": "active",
"type": "category",
"field_id": 148215928,
"label": "Contract Type",
"values": [{
"value": {
"status": "active",
"text": "No Contract",
"id": 4,
"color": "DDDDDD"
}
}],
"config": {
"default_value": null,
"unique": false,
"description": null,
"hidden_create_view_edit": false,
"required": true,
"mapping": null,
"label": "Contract Type",
"visible": true,
"delta": 7,
"hidden": false,
"settings": {
"multiple": false,
"options": [{
"status": "active",
"text": "PO \/ Purchase \/ T&M",
"id": 1,
"color": "FFD5C2"
}, {
"status": "active",
"text": "Original Contract",
"id": 2,
"color": "D2E4EB"
}, {
"status": "active",
"text": "Service Rider",
"id": 3,
"color": "DCEBD8"
}, {
"status": "active",
"text": "No Contract",
"id": 4,
"color": "DDDDDD"
}],
"display": "dropdown"
}
},
"external_id": "contract-type"
}, {
"status": "active",
"type": "category",
"field_id": 93034840,
"label": "Instal Status",
"values": [{
"value": {
"status": "active",
"text": "Closed",
"id": 13,
"color": "E1D8ED"
}
}],
"config": {
"default_value": null,
"unique": false,
"description": null,
"hidden_create_view_edit": false,
"required": true,
"mapping": null,
"label": "Instal Status",
"visible": true,
"delta": 13,
"hidden": false,
"settings": {
"multiple": false,
"options": [{
"status": "deleted",
"text": "To Be Reviewed",
"id": 2,
"color": "F7F0C5"
}, {
"status": "deleted",
"text": "ACS Accounting Review",
"id": 5,
"color": "FFD5C2"
}, {
"status": "deleted",
"text": "Ignite Setup",
"id": 10,
"color": "DCEBD8"
}, {
"status": "active",
"text": "To Be Scheduled",
"id": 8,
"color": "D2E4EB"
}, {
"status": "active",
"text": "In Progress",
"id": 6,
"color": "DCEBD8"
}, {
"status": "active",
"text": "Warranty",
"id": 11,
"color": "F7F0C5"
}, {
"status": "active",
"text": "Complete",
"id": 3,
"color": "D1F3EC"
}, {
"status": "deleted",
"text": "Closed",
"id": 7,
"color": "DDDDDD"
}, {
"status": "active",
"text": "Cancelled",
"id": 9,
"color": "DDDDDD"
}, {
"status": "deleted",
"text": "VA in Progress",
"id": 4,
"color": "E1D8ED"
}, {
"status": "deleted",
"text": "Submitted",
"id": 1,
"color": "F7F0C5"
}, {
"status": "active",
"text": "On Hold",
"id": 12,
"color": "F7D1D0"
}, {
"status": "active",
"text": "Closed",
"id": 13,
"color": "E1D8ED"
}],
"display": "inline"
}
},
"external_id": "status"
}],
"initial_revision": {
"item_revision_id": 1664054437,
"created_via": {
"url": null,
"auth_client_id": 25162,
"display": true,
"name": "importer",
"id": 25162
},
"created_by": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"created_on": "2017-07-10 15:31:16",
"user": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"type": "creation",
"revision": 0
},
"current_revision": {
"item_revision_id": 1664054437,
"created_via": {
"url": null,
"auth_client_id": 25162,
"display": true,
"name": "importer",
"id": 25162
},
"created_by": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"created_on": "2017-07-10 15:31:16",
"user": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"type": "creation",
"revision": 0
},
"linked_account_id": null,
"push": {
"timestamp": 1499700676,
"expires_in": 21600,
"channel": "\/item\/641331142",
"signature": "b8a816ff367da6bc730071c875ca3fdca2d2c344"
},
"external_id": null
}
Have you tried http://podio.github.io/podio-php/fields/#category-field ?
Setting values
Set a single value by using the option_id. You can also
add a value with add_value()
$item = PodioItem::get_basic(123);
$field_id = 'category';
// Set value to a single option
$item->fields[$field_id]->values = 4; // option_id=4
// Add value to existing selection
$item->fields[$field_id]->add_value(4); // option_id=4
Use an array to set multiple values
$item = PodioItem::get_basic(123);
$field_id = 'category';
$item->fields[$field_id]->values = array(4,5,6); // option_ids: 4, 5 and 6
Creating item with value:
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>array(13)]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
This is my json object. How do I loop through test and testing values inside data_options->"key"->options and show them in checkbox values?
{
"data": {
"0": {
"id": 2,
"survey_id": 14,
"question": "What's here?",
"question_type": 2,
"created_at": "2016-07-15 18:29:05",
"updated_at": "2016-07-15 18:29:05",
"deleted_at": null,
"type": "checkbox",
"ques_options_id": 2,
"key": "pqr",
"value": "PQR",
"survey_user_id": 39
},
"1": {
"id": 9,
"survey_id": 14,
"question": "Whats demo? ",
"question_type": 4,
"created_at": "2016-07-15 18:29:05",
"updated_at": "2016-07-15 18:29:05",
"deleted_at": null,
"type": "dropdown",
"ques_options_id": 9,
"key": "checking",
"value": "Checking",
"survey_user_id": 39
},
"2": {
"id": 1,
"survey_id": 14,
"question": "What's in your mind?",
"question_type": 1,
"created_at": "2016-07-15 18:29:05",
"updated_at": "2016-07-15 18:29:05",
"deleted_at": null,
"type": "textarea",
"ques_options_id": 1,
"key": "abc",
"value": "ABC",
"survey_user_id": 39
},
"3": {
"id": 8,
"survey_id": 14,
"question": "Whats abc here?",
"question_type": 3,
"created_at": "2016-07-15 18:29:05",
"updated_at": "2016-07-15 18:29:05",
"deleted_at": null,
"type": "radio",
"ques_options_id": 8,
"key": "abcde",
"value": "ABCDEF",
"survey_user_id": 39
},
"4": {
"id": 10,
"survey_id": 14,
"question": "Testing here?",
"question_type": 2,
"created_at": "2016-07-15 18:29:05",
"updated_at": "2016-07-15 18:29:05",
"deleted_at": null,
"type": "checkbox",
"ques_options_id": 10,
"key": "test",
"value": "Test",
"survey_user_id": 39
},
"5": {
"id": 10,
"survey_id": 14,
"question": "Testing here?",
"question_type": 2,
"created_at": "2016-07-15 18:29:05",
"updated_at": "2016-07-15 18:29:05",
"deleted_at": null,
"type": "checkbox",
"ques_options_id": 10,
"key": "Testing",
"value": "Testing",
"survey_user_id": 39
},
"data_option": {
"1": {
"options": {
"0": {
"abc": "ABC"
}
},
"question": "What's in your mind?",
"type": "textarea"
},
"2": {
"options": {
"0": {
"pqr": "PQR"
}
},
"question": "What's here?",
"type": "checkbox"
},
"8": {
"options": {
"0": {
"abcde": "ABCDEF"
}
},
"question": "Whats abc here?",
"type": "radio"
},
"9": {
"options": {
"0": {
"checking": "Checking"
}
},
"question": "Whats demo? ",
"type": "dropdown"
},
"10": {
"options": {
"0": {
"test": "Test"
},
"1": {
"Testing": "Testing"
}
},
"question": "Testing here?",
"type": "checkbox"
}
}
}
}
First convert object to array and then you can use ng-repeat on array as below:
var myObj = {
0: {
"id": 1,
"survey_id": 12
},
1: {
"id": 2,
"survey_id": 14
},
};
var array = $.map(myObj, function(value, index) {
return [value];
});
You can do this in view,
<div ng-app="myApp" ng-controller="ListCtrl">
<div ng-repeat="patient in data.data.data_option ">
<div ng-repeat="detail in patient.options ">
<input type="checkbox">
<span class="done-{{todo.done}}">{{detail}}</span>
</div>
</div>
Working Application