filtering json data in php laravel - php

I've a sample json data
"regions": [
{
"id": 1,
"name": "Region 1",
"state_id": 1,
"areas" :[ {
"id": 1,
"name": "area 1",
"region_id": 1},
{
"id": 2,
"name": "area 2",
"region_id": 1}
]
},
{
"id": 2,
"name": "Region 2",
"state_id": 1,
"areas" :[ {
"id": 3,
"name": "area 3",
"region_id": 2},
{
"id": 4,
"name": "area 4",
"region_id": 2}
]
}
]
How can I filter out the data the based on id in the regions? For example, if id is 2, then the response should be like
"regions": [
{
"id": 2,
"name": "Region 2",
"state_id": 1,
"areas" :[ {
"id": 3,
"name": "area 3",
"region_id": 2},
{
"id": 4,
"name": "area 4",
"region_id": 2}
]
}
]

You can json_decode that json string and then use array_filter method to filter regions
$regions = json_decode('{"regions":
[
{
"id": 1,
"name": "Region 1",
"state_id": 1,
"areas" :[ {
"id": 1,
"name": "area 1",
"region_id": 1
},{
"id": 2,
"name": "area 2",
"region_id": 1
}]
},{
"id": 2,
"name": "Region 2",
"state_id": 1,
"areas" :[{
"id": 3,
"name": "area 3",
"region_id": 2
},{
"id": 4,
"name": "area 4",
"region_id": 2
}]
}
]
}', true);
// Filter Region
$region_id = 2;
$filtered_regions = array_filter($regions['regions'], function($r) use ($region_id) {
return $r['id'] == $region_id;
});
// Filter By Area Id
$area_id = 2;
$filtered_areas = array_filter($regions['regions'], function($r) use ($area_id) {
$areas = array_filter($r['areas'], function($area) use ($area_id) {
return $area['id'] == $area_id;
});
return count($areas) > 0;
});
return ['regions' => $filtered_regions];

First apply json_decode on your json data then apply filter . If you want to get an whole object from this response array , then write a function which will accept the id and return that object after filtering.

Related

how to eager loading nested relations and not repeat them at top level?

a have a comments table that has these fields: id, body, parent_id
each comment can have comments, that's why it has a parent_id column.
class Comment extends Model
{
public function comments()
{
return $this->HasMany($this, 'parent_id');
}
}
It can have child comments up to two levels only.
In my controller I retrieve the information this way:
return Comment::with('comments.comments')->get();
The resulting response is like this:
[
{
"id": 1,
"body": "test",
"parent_id": null,
"comments": [
{
"id": 2,
"body": "test",
"parent_id": 1,
"comments": [
{
"id": 3,
"body": "test",
"parent_id": 2
},
{
"id": 4,
"body": "test",
"parent_id": 2
}
]
},
{
"id": 5,
"body": "test",
"parent_id": 1,
"comments": []
}
]
},
{
"id": 2,
"body": "test",
"parent_id": 1,
"comments": [
{
"id": 3,
"body": "test",
"parent_id": 2,
"comments": []
},
{
"id": 4,
"body": "test",
"parent_id": 2,
"comments": []
}
]
},
{
"id": 3,
"body": "test",
"parent_id": 2,
"comments": []
},
{
"id": 4,
"body": "test",
"parent_id": 2,
"comments": []
},
{
"id": 5,
"body": "test",
"parent_id": 1,
"comments": []
},
{
"id": 6,
"body": "test",
"parent_id": null,
"comments": []
}
]
What I would to retrive is this:
[
{
"id": 1,
"body": "test",
"parent_id": null,
"comments": [
{
"id": 2,
"body": "test",
"parent_id": 1,
"comments": [
{
"id": 3,
"body": "test",
"parent_id": 2
},
{
"id": 4,
"body": "test",
"parent_id": 2
}
]
},
{
"id": 5,
"body": "test",
"parent_id": 1,
"comments": []
}
]
},
{
"id": 6,
"body": "test",
"parent_id": null,
"comments": []
}
]
I would like that the child comments don't repeat again at top level, what can I do?
thank you.
Add whereNull(‘parent_id’) to your top level query.
return Comment::whereNull('parent_id')->with('comments.comments')->get();

Skipping an object if one of the attributes is null in laravel

Task: you need to get a students object, with the conditions "group_id*" and "specialty_*id", where all these 3 tables are connected. It is also necessary to paginate only those objects of students for whom a group and specialty was found.
My problem: if the group or specialty data is not found, then the object is still added with the "group: null" attribute to pagination, thereby failing to display the requested number of elements on the page. How can this problem be solved?
The code:
$students = Student::with([
"group" =>
function($query) {
$query->where("id", \request("group_id"));
},
"group.speciality" =>
function($query) {
$query->where("id", \request("speciality_id"));
},
])->paginate(\request("page_size") ? : 10)->toArray();
return response()->json($students);
Preview: when I make a request with parameters
group_id = 2
speciality_id = 2
page_size = 2
The following object is returned:
{
"current_page": 1,
"data": [
{
"id": 1,
"receipt_date": "2010-11-02",
"user": {
"id": 1,
"login": "ykirillova#gmail.com",
"phone": "+7 (922) 472-9240",
"role": "user",
"passport": {
"series": 1762,
"number": 384282,
"date_of_issue": "1991-11-27",
"issued": "magni",
"division_code": 3,
"scan": "*photo link*",
"secondname": "Kilikova",
"firstname": "Olga",
"thirdname": "Anisimova",
"birthday": "1973-05-13",
"sex": "W"
}
},
"group": {
"id": 2,
"group_code": "4433",
"speciality": {
"id": 2,
"specialty_title": "Programming in computer systems",
"faculty": "SPO IKTZI"
}
}
},
{
"id": 2,
"receipt_date": "1973-11-07",
"user": {
"id": 2,
"login": "marta.fedorov#dackov.net",
"phone": "+7 (922) 903-0339",
"role": "user",
"passport": {
"series": 8241,
"number": 419233,
"date_of_issue": "1980-06-05",
"issued": "quos",
"division_code": 33,
"scan": "*photo link*",
"secondname": "Efremov",
"firstname": "Boleslav",
"thirdname": "Kostin",
"birthday": "2009-04-03",
"sex": "W"
}
},
"group": null
}
],
"per_page": "2",
"total": 75
}
Whereas with group == null, only 2 objects with groups should be returned
{
"current_page": 1,
"data": [
{
"id": 1,
"receipt_date": "2010-11-02",
"user": {
"id": 1,
"login": "ykirillova#gmail.com",
"phone": "+7 (922) 472-9240",
"role": "user",
"passport": {
"series": 1762,
"number": 384282,
"date_of_issue": "1991-11-27",
"issued": "magni",
"division_code": 3,
"scan": "*photo link*",
"secondname": "Kilikova",
"firstname": "Olga",
"thirdname": "Anisimova",
"birthday": "1973-05-13",
"sex": "W"
}
},
"group": {
"id": 2,
"group_code": "4433",
"speciality": {
"id": 2,
"specialty_title": "Programming in computer systems",
"faculty": "SPO IKTZI"
}
}
},
{
"id": 5,
"receipt_date": "2002-07-05",
"user": {
"id": 5,
"login": "tester#mail.ru",
"phone": "+7 (800) 555-3535",
"role": "user",
"passport": {
"series": 5521,
"number": 866521,
"date_of_issue": "1980-06-05",
"issued": "quos",
"division_code": 33,
"scan": "*photo link*",
"secondname": "Pavlov",
"firstname": "Denis",
"thirdname": "Artemev",
"birthday": "2009-04-03",
"sex": "W"
}
},
"group": {
"id": 2,
"group_code": "4433",
"speciality": {
"id": 2,
"specialty_title": "Programming in computer systems",
"faculty": "SPO IKTZI"
}
}
}
],
"per_page": "2",
"total": 75
}
The data presented is irrelevant as it is randomly generated.
Removing data elements in a loop with group equal to null does not solve the problem, since pagination gets lost.

Laravel mongodb search data in object

I am building a Laravel app that connected with MongoDB. Now I am facing an issue when query data from the database.
table name: Employee
[
{
"_id": 1,
"name": "user 1",
"managers": [
{
"id": 321,
"user_id": 1,
"ack_groups: [
["2","3", "4"],
["2", "5", "6"]
]
},
......
]
},
{
"_id": 2,
"name": "user 2",
"managers": [
{
"id": 3213,
"user_id": 2,
"ack_groups: [
["6","7", "8"],
["2", "5", "6"]
]
},
......
]
},
{
"_id": 3,
"name": "user 3",
"managers": [
{
"id": 321,
"user_id": 3,
"ack_groups: [
["8","9", "14"],
["12", "15", "16"]
]
},
......
]
},
]
I am trying to query list of employees data from Employee table with some conditions. The condition is that I want to get list of employees where ac_groups in managers equal to 2.
Then the output should be:
[
{
"_id": 1,
"name": "user 1",
"managers": [
{
"id": 321,
"user_id": 1,
"ack_groups: [
["2","3", "4"],
["2", "5", "6"]
]
},
......
]
},
{
"_id": 2,
"name": "user 2",
"managers": [
{
"id": 3213,
"user_id": 2,
"ack_groups: [
["6","7", "8"],
["2", "5", "6"]
]
},
......
]
},
]
What I have tried in code: Employee::where('verify_routes.managers.ack_groups', '=', "2")->get();
Forum: "Find in array value"
Doc: Specific Operators
This should work:
Employee::where('verify_routes.managers.ack_groups', 'all', [2])->get();

How to merge two Arrays/Json with special conditions - PHP

i need to combine two jsons in a job task, according to the person's location. I have a Json with people from location type 1, and another from location type 2.
I need to combine in a new array for each time they have the same location, and they are repeated and separate according to type. I tried to do it with a foreach inside the other, but with no success. Here is an example of JSON.
Json 1:
[
{
"id": 1,
"name": "Jacob",
"place": "Brazil",
"type": 1
},
{
"id": 2,
"name": "Izac",
"place": "Brazil",
"type": 1
},
{
"id": 3,
"name": "Anran",
"place": "Brazil",
"type": 1
},
{
"id": 4,
"name": "Irbur",
"place": "Brazil",
"type": 1
},
{
"id": 5,
"name": "Lusos",
"place": "Brazil",
"type": 1
},
{
"id": 6,
"name": "Gamio",
"place": "Brazil",
"type": 1
},
{
"id": 7,
"name": "Nubeil",
"place": "Brazil",
"type": 1
},
{
"id": 8,
"name": "Usgon",
"place": "Brazil",
"type": 1
},
{
"id": 15,
"name": "Fikis",
"place": "England",
"type": 1
}
]
Json 2:
[
{
"id": 9,
"name": "Gipin",
"place": "Brazil",
"type": 0
},
{
"id": 10,
"name": "Paoir",
"place": "Brazil",
"type": 0
},
{
"id": 11,
"cia": "Mutue",
"place": "Brazil",
"type": 0
},
{
"id": 12,
"name": "Ziefel",
"place": "England",
"type": 0
},
{
"id": 13,
"name": "Liedo",
"place": "England",
"type": 0
},
{
"id": 14,
"name": "Vicis",
"place": "England",
"type": 0
}
]
the result might look something like this:
{
"groups": [ // groups (same place)
{
"tipe1": [ // groups type 1: same place
{
"id": 1,
"name": "Jacob",
"place": "Brazil"
},
{
"id": 1,
"name": "Jacob",
"place": "Brazil"
}
]
"tipe2": [ // groups type 2: same place
{
"id": 11,
"name": "Mutue",
"place": "Brazil"
},
]
},
{
"tipe1": [
{
"id": 15,
"name": "Fikis",
"place": "England"
}
]
"tipe2": [
{
"id": 14,
"name": "Vicis",
"place": "England"
},
]
}
],
}
The rule for grouping is, two or more items of type 1 can be grouped with 1 of type 2, in reverse as well. It can be thought of as outward flights, and back flights.

Rejecting certain index parameters in an array

Given an array, in which each index there are certain key value pairs, how do I select certain key value pairs and reject the others.
$channels = Channel::get()->toArray();
This will yield the following Array:
"channels": [
{
"id": 1,
"name": "Info Release",
"slug": "info-release",
"desc": "Contains blah blah.",
"access_level": "0",
"created_at": "2018-12-02 01:23:50",
"updated_at": "2018-12-05 07:54:41"
},
{
"id": 11,
"name": "Casual News",
"slug": "casual-news",
"desc": "Contains blah blah.",
"access_level": "0",
"created_at": "2018-12-05 05:34:50",
"updated_at": "2018-12-05 07:54:32"
},
{
"id": 12,
"name": "haha",
"slug": "haha",
"desc": "Contains blah blah.",
"access_level": "0",
"created_at": "2018-12-29 23:27:16",
"updated_at": "2018-12-29 23:27:16"
}
],
What is the best way to turn that array into this:
"channels": [
{
"id": 1,
"name": "Information Release",
"slug": "information-release",
},
{
"id": 11,
"name": "Casual News",
"slug": "casual-news",
},
{
"id": 12,
"name": "haha",
"slug": "haha",
}
],
So that if I write
$channels[0]->id
it will spit out 1
Can You try this
$viewFileds = ['id', 'name', 'slug'];
$channels = Channel::get()
->map(function ($each) use ($viewFileds) {
return Arr::only($each, $viewFileds);
});
It will return the new Collection by Only the field enabled in the $viewFileds
Hope it helps

Categories