I am struggling to come up with a way to query this. I have a model that displays a resource with its children and the children are from the same table itself which means the resource tree is unlimited nesting. the relation is as follows
class UserDrive extends Model
{
public function children(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(self::class, 'folder_id','id')->with('children');
}
}
and my controller calls it like shown below
public function getFolder($folder_id)
{
return new UserDriveCollection(UserDrive::with("children")->where('user_id', $this->user)->where('id', $folder_id)->paginate($this->paginatePerPage(), ['*'], 'page', 1));
}
the resource looks like below
public function toArray($request)
{
return [
"id" => $this->id,
"folder_name" => $this->folder_name,
"children" => self::collection($this->whenLoaded('children'))
];
}
and the results are in the following format
[
{
"id": 1,
"folder_name": "Setswana Material",
"children": [
{
"id": 2,
"folder_name": "Love Materials",
"children": [
{
"id": 11,
"folder_name": "folder54",
"children": [
{
"id": 19,
"folder_name": "folder1",
"children": []
},
{
"id": 20,
"folder_name": "folder2",
"children": []
},
{
"id": 21,
"folder_name": "folder",
"children": []
},
{
"id": 22,
"folder_name": "folder3",
"children": []
},
{
"id": 23,
"folder_name": "folder4",
"children": []
},
{
"id": 24,
"folder_name": "folder5",
"children": []
}
]
},
{
"id": 12,
"folder_name": "folder5",
"children": []
},
{
"id": 13,
"folder_name": "\nfolder5",
"children": []
},
{
"id": 14,
"folder_name": "folder",
"children": []
},
{
"id": 15,
"folder_name": "folder4",
"children": []
},
{
"id": 16,
"folder_name": "folder3",
"children": []
},
{
"id": 17,
"folder_name": "folder2",
"children": []
},
{
"id": 18,
"folder_name": "folder1",
"children": []
}
]
},
{
"id": 6,
"folder_name": "folder1",
"children": []
},
{
"id": 7,
"folder_name": "folder2",
"children": []
},
{
"id": 8,
"folder_name": "folder3",
"children": []
},
{
"id": 9,
"folder_name": "folder4",
"children": []
},
{
"id": 10,
"folder_name": "folder5",
"children": []
}
]
},
{
"id": 3,
"folder_name": "folder2",
"children": []
},
{
"id": 4,
"folder_name": "folder3",
"children": []
},
{
"id": 5,
"folder_name": "folder4",
"children": []
}
]
so my question is how do I check if a resource exists as a child or as a child of a child or even deeper than that. the relationship/tree array is unlimited so I want to check for all possible children.
so if I want to check if id: 24 is a child of id: 1 it should be true as of the data above. and if I want to check if id: 2 is a child of id: 1 if should be true as well but if I check id: 5 is a child of id: 1 it should be false because it is not a child under it.
for those who may want to do something similar. this is was my solution to the problem.
sample data 👇👇 covert to php array
[
{
"id": 1,
"folder_name": "Setswana Material",
"children": [
{
"id": 2,
"folder_name": "Love Materials",
"children": [
{
"id": 11,
"folder_name": "folder54",
"children": [
{
"id": 19,
"folder_name": "folder1",
"children": []
},
{
"id": 20,
"folder_name": "folder2",
"children": []
},
{
"id": 21,
"folder_name": "folder",
"children": []
},
{
"id": 22,
"folder_name": "folder3",
"children": []
},
{
"id": 23,
"folder_name": "folder4",
"children": []
},
{
"id": 24,
"folder_name": "folder5",
"children": []
}
]
},
{
"id": 12,
"folder_name": "folder5",
"children": []
},
{
"id": 13,
"folder_name": "\nfolder5",
"children": []
},
{
"id": 14,
"folder_name": "folder",
"children": []
},
{
"id": 15,
"folder_name": "folder4",
"children": []
},
{
"id": 16,
"folder_name": "folder3",
"children": []
},
{
"id": 17,
"folder_name": "folder2",
"children": []
},
{
"id": 18,
"folder_name": "folder1",
"children": []
}
]
},
{
"id": 6,
"folder_name": "folder1",
"children": []
},
{
"id": 7,
"folder_name": "folder2",
"children": []
},
{
"id": 8,
"folder_name": "folder3",
"children": []
},
{
"id": 9,
"folder_name": "folder4",
"children": []
},
{
"id": 10,
"folder_name": "folder5",
"children": []
}
]
},
{
"id": 3,
"folder_name": "folder2",
"children": []
},
{
"id": 4,
"folder_name": "folder3",
"children": []
},
{
"id": 5,
"folder_name": "folder4",
"children": []
}
]
and the code to get ids from the code
return $resource->pipe(function ($collection) {
$array = $collection->toArray();
$ids = [];
array_walk_recursive($array, function ($value, $key) use (&$ids) {
if ($key === 'id') {
$ids[] = $value;
};
});
return $ids;
});
they results are in the following format
[
2,11,19,20,21,22,23,24,12,13,14,15,16,17,18
]
it gets all the id: fields from the multidimentional array and I tested for depth of up to 8 steps deep in, it is able to get all the id:
i have an json response like this :
{
"result": [
{
"id": 2984,
"amount": 5000000,
"account": "money",
"trade_no": "2121683414670617655",
"type": 0,
"for_id": 0,
"created_at": "2021-02-16 20:50:14",
"payment_method": 0,
"status": 1
},
{
"id": 2999,
"amount": -450000,
"account": "money",
"trade_no": "212173166272246C118",
"type": 5,
"for_id": "2021021760479",
"created_at": "2021-01-17 10:14:22",
"payment_method": "0",
"status": 1
},
],
"code": 200,
"description": "OK"
}
and then i want grouping the api by month and year look like this :
please help me to solve this
You can use groupBy method from lodash.
var grouped = _.groupBy(result, function(item) {
return item.created_at.substring(0,7);
});
This will give you data in below format:
{
"2021-02": [
{
"id": 2984,
"amount": 5000000,
"account": "money",
"trade_no": "2121683414670617655",
"type": 0,
"for_id": 0,
"created_at": "2021-02-16 20:50:14",
"payment_method": 0,
"status": 1
}
],
"2021-01": [
{
"id": 2999,
"amount": -450000,
"account": "money",
"trade_no": "212173166272246C118",
"type": 5,
"for_id": "2021021760479",
"created_at": "2021-01-17 10:14:22",
"payment_method": "0",
"status": 1
}
]
}
When rendering the data, you can format your Year-Month key to string using moment like this,
moment('2021-01', 'YYYY-MM').format('MMMM YYYY');
How merge duplicate object and add price like it has tree dublicate then price will add up three time and return new array/obj. this is obj here obj. and also keep track how many duplicates are there
"id1": Object {
"date": 1609247265357,
"orders": Array [
Object {
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/jsa/128.jpg",
"id": 3,
"name": "Tomato",
"price": 15,
},
Object { //this
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/talhaconcepts/128.jpg",
"id": 4,
"name": "Ginger",
"price": 12,
},
Object { //and this
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/talhaconcepts/128.jpg",
"id": 4,
"name": "Ginger",
"price": 12,
},
Object {
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/jsa/128.jpg",
"id": 3,
"name": "Tusi",
"price": 15,
},
],
"uid": "EKSjIptE7Ma32SyGylhKBmLLJHB2",
},......and so on
This is my first attempt at creating a react native project (with redux as well) and I have a question relating to passing down info. I have an API that looks like this :
[
{
"id": 1,
"name": "Module 1",
"topics": [
{
"id": 1,
"name": "Iterators and Helpers",
"course_id": 1
},
{
"id": 2,
"name": "Object Oriented Ruby",
"course_id": 1
},
{
"id": 3,
"name": "One to Many Relationships",
"course_id": 1
},
{
"id": 4,
"name": "Many to Many Relationships",
"course_id": 1
},
{
"id": 5,
"name": "Active Record Basics",
"course_id": 1
},
{
"id": 2,
"name": "Module 2",
"topics": [
{
"id": 9,
"name": "Introduction to Sinatra",
"course_id": 2
},
{
"id": 10,
"name": "Sinatra CRUD",
"course_id": 2
},
{
"id": 11,
"name": "Sinatra Associations",
"course_id": 2
},
{
"id": 12,
"name": "Introduction to Rails",
"course_id": 2
},
{
"id": 13,
"name": "Rails Forms + CRUD",
"course_id": 2
},
{
"id": 14,
"name": "Rails Associations + CRUD",
"course_id": 2
},
{
"id": 15,
"name": "Rails Sessions and Cookies",
"course_id": 2
},
{
"id": 16,
"name": "Rails Authentication and Authorization",
"course_id": 2
},
{
"id": 17,
"name": "Module 2 Project Guidelines",
"course_id": 2
}
]
{
"id": 3,
"name": "Module 3",
"topics": [
{
"id": 18,
"name": "Introduction to JavaScript",
"course_id": 3
},
{
"id": 19,
"name": "Introduction to Document Object Model",
"course_id": 3
},
{
"id": 20,
"name": "JavaScript Events",
"course_id": 3
},
{
"id": 21,
"name": "JavaScript Asynchronous Functionality + Fetch & CRUD",
"course_id": 3
Each 'module' has many topics. I want the Modules to appear on one screen and the topics for each course to appear once a button is clicked. I have this so far:
render(){
console.log(this.props.courseNames)
return(
<FlatList style={styles.flatlist} keyExtractor={(item)=> item.id} data={this.props.courseNames} ItemSeparatorComponent = { this.FlatListItemSeparator }
renderItem={({item}) => {
return <TouchableOpacity><Button title={item.name} onPress={() => this.props.navigation.navigate('Topics')} style={styles.listitem}/></TouchableOpacity>
}}
ListHeaderComponent = {this.header}/>
)
}
}
This is just a snippet but I'm wondering how I can pass down the information specific to that class so that I can retrieve all the topics that it contains and display them on the next screen. Any pointers would be great! thanks!
I think you're asking how to pass 'parameters' to navigation routes, which, yes, do render components that can receive props from the navigation route parameter.
In your case,
this.props.navigation.navigate('Topics')
should be changed to;
this.props.navigation.navigate('Topics', {id:item.id})
Once you navigate to your Topics route, that component will be able to read the 'id' value from;
props.route.params.id
Route parameters will be any object passed after the route name in this.props.navigation.navigate method.