How to access a field in relation in eloquent orm - database

I wrote this code to get the product information with it's images and category:
->where('category_id', 5)
->with('category', 'Files')->get();
my result is:
{
"id": 2,
"name": "test",
"price": 13000,
"description": "some text ...",
"shop_id": 1,
"rate": 0,
"category_id": 5,
"discount_percent": 20,
"category": {
"id": 5,
"name": "cat1",
"shop_id": 1
},
"files": [
{
"id": 99,
"disk_name": "5ef1af07d6d98778754621.jpg",
"file_name": "13330983_xl.jpg",
"file_size": 69813,
"content_type": "image/jpeg",
"title": null,
"description": null,
"field": "product_gallery",
"sort_order": 99,
"created_at": "2020-06-23 07:28:07",
"updated_at": "2020-06-23 07:28:10",
"path":...... storage/app/uploads/public/5ef/1af/07d/5ef1af07d6d98778754621.jpg",
"extension": "jpg"
}
]
}
now i want to access the path field, how can i do it?
i use this way for access but i don't get result:
products[0].files[0].path

You should use toArray() function to convert data likes this
->where('category_id', 5)
->with('category', 'Files')->get()->toArray();
And then access
products[0]['files'][0]['path']

Related

How to scroll to a particular section of the data fetched from an API while filtering the data on some parameter?

I am calling an API from a server, and I am getting this as a response. A part of the data I am getting -
{
"id": 322854,
"date": "2022-09-20T18:00:00+00:00",
"time": "18:00",
"timestamp": 1663696800,
"timezone": "UTC",
"stage": null,
"week": null,
"status": {
"long": "Game Finished",
"short": "FT",
"timer": null
},
"league": {
"id": 9,
"name": "French Cup",
"type": "cup",
"season": 2022,
"logo": "https://media-2.api-sports.io/basketball/leagues/9.png"
},
"country": {
"id": 4,
"name": "France",
"code": "FR",
"flag": "https://media-2.api-sports.io/flags/fr.svg"
},
"teams": {
"home": {
"id": 22,
"name": "Boulazac",
"logo": "https://media-2.api-sports.io/basketball/teams/22.png"
},
"away": {
"id": 2294,
"name": "CEP Lorient",
"logo": "https://media-2.api-sports.io/basketball/teams/2294.png"
}
},
"scores": {
"home": {
"quarter_1": 16,
"quarter_2": 12,
"quarter_3": 21,
"quarter_4": 26,
"over_time": null,
"total": 75
},
"away": {
"quarter_1": 9,
"quarter_2": 16,
"quarter_3": 19,
"quarter_4": 23,
"over_time": null,
"total": 67
}
}
}
{
"id": 322854,
"date": "2022-09-20T18:00:00+00:00",
"time": "18:00",
"timestamp": 1663696800,
"timezone": "UTC",
"stage": null,
"week": null,
"status": {
"long": "Game Not Started",
"short": "NS",
"timer": null
},
"league": {
"id": 9,
"name": "French Cup",
"type": "cup",
"season": 2022,
"logo": "https://media-2.api-sports.io/basketball/leagues/9.png"
},
"country": {
"id": 4,
"name": "France",
"code": "FR",
"flag": "https://media-2.api-sports.io/flags/fr.svg"
},
"teams": {
"home": {
"id": 22,
"name": "Boulazac",
"logo": "https://media-2.api-sports.io/basketball/teams/22.png"
},
"away": {
"id": 2294,
"name": "CEP Lorient",
"logo": "https://media-2.api-sports.io/basketball/teams/2294.png"
}
},
"scores": {
"home": {
"quarter_1": 16,
"quarter_2": 12,
"quarter_3": 21,
"quarter_4": 26,
"over_time": null,
"total": 75
},
"away": {
"quarter_1": 9,
"quarter_2": 16,
"quarter_3": 19,
"quarter_4": 23,
"over_time": null,
"total": 67
}
}
}
There are a bunch of such objects in an array each having a different fixture. As you can see that status?.short === "NS" in one while the other is status?.short === "FT". Now, I want to filter the data using these properties.
<div className="fixturesTab-container">
<div className="fixturesTab-container__header">
<h4>Fixtures</h4>
</div>
<div className="fixturesTab-container__body">
{fixtures.map((fixture) => (
<div
key={fixture?.id}
className="fixturesTab-container__body__box"
>
......
</div>
))}
</div>
</div>
I want the data to be displayed in such a manner that when the user browse to that page and the API is being fetched, eventhough the entire data (fixtures) is being displayed in the page. I want the fixtures that have not yet started, i.e has the property of status?.short === "NS", to be displayed in the window that is visible to the user. And the rest data can be acquired by either scrolling up, which will have the fixtures that have been completed, or by scrolling down which will have the fixtures that have not yet been started.
If, I am unable to understand what I want properly, then you can check out this page . This is exactly what I am planning to achieve.

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 we can show collection data in array

When I am using get api and get the data in json format
{
"uuid": "46d00217-6e35-485c-ac20-c204a8a24a68",
"name": "AMan",
"dispute_location": "Hyder",
"max_dispute_value": "200",
"min_dispute_value": "100",
"state": "U.p",
"district": "morene",
"calendar_id": "3",
"description": null,
"subject_matters": [
{
"id": 1,
"name": "A",
"created_at": "2020-08-14T12:24:52.000000Z",
"updated_at": "2020-08-14T12:24:52.000000Z",
"pivot": {
"court_uuid": "46d00217-6e35-485c-ac20-c204a8a24a68",
"subject_matter_id": 1
}
},
{
"id": 2,
"name": "B",
"created_at": "2020-08-14T12:24:57.000000Z",
"updated_at": "2020-08-14T12:24:57.000000Z",
"pivot": {
"court_uuid": "46d00217-6e35-485c-ac20-c204a8a24a68",
"subject_matter_id": 2
}
}
]
}
but i want subject_matters data in this format
"subject_matters": [1,2]
Api resource (court resources)
You can use ->pluck('id'); in your resource.
return [
'subject_matters_id' => $this->subjectMatters->pluck('id')
]
It should return an array of subject matter IDs.

How to get separate dictionary of each record from list of dictionaries without using multiple loop?

In current data "children" key will be fix. If there is any child data available then there then it must in list of dictionary format.
If there is no any children available then it no "children" key is available in dictionary.
I don't want to use the loop to bifurcate this data.
I want the same consistent sequence data. Please note there will any number of hierarchy available.
I want all this data in list of dictionary format like given requirement data example.
Current data.
{
"id": 2,
"parent_id": 1,
"name": "0",
"is_active": true,
"position": 1,
"level": 1,
"children": [
{
"id": 8,
"parent_id": 1,
"name": "01",
"is_active": false,
"position": 1,
"level": 2,
"children": [
"id": 9,
"parent_id": 1,
"name": "010",
"is_active": false,
"position": 1,
"level": 2,
"children": [
<'Here N number of hirerchy availabe'>
]
]
},
],
"id": 3,
"parent_id": 1,
"name": "1",
"is_active": true,
"position": 1,
"level": 1,
"children": [
{
"id": 5,
"parent_id": 1,
"name": "03",
"is_active": false,
"position": 1,
"level": 2,
"children": [
"id": 6,
"parent_id": 1,
"name": "030",
"is_active": false,
"position": 1,
"level": 2,
"children": [
<'Here N number of hirerchy availabe'>
]
]
},
]
}
Requirement.
[{
"id": 2,
"parent_id": 1,
"name": "0",
"is_active": true,
"position": 1,
"level": 1,
},
{
"id": 3,
"parent_id": 1,
"name": "01",
"is_active": false,
"position": 1,
"level": 2,
},
{
"id": 3,
"parent_id": 1,
"name": "01",
"is_active": false,
"position": 1,
"level": 2,
},{
<N Number of dictionary data with consistant sequence>
}]
The suitable answer will definitely acceptable.
You can flatten the given nested data structure with a recursive function like this:
def flatten(data):
if isinstance(data, dict):
return [data, *flatten(data.pop('children', ()))]
return [subrecord for record in data for subrecord in flatten(record)]
Demo: https://repl.it/#blhsing/BlankHatefulResources
I have found the solution to my question. Below code is working for me.
if isinstance(categories, dict):
values = {
'name': categories.get('name'),
'parent_id': categories.get('parent_id'),
'magento_id': categories.get('id'),
'instance_id': instance.id
}
self.category_list.append(values)
self._analyse_response_data(categories.get('children_data'), instance)
if isinstance(categories, list):
for category in categories:
values = {
'name': category.get('name'),
'parent_id': category.get('parent_id'),
'magento_id': category.get('id'),
'instance_id': instance.id
}
self.category_list.append(values)
self._analyse_response_data(category.get('children_data'), instance)
return self.category_list
I have used recursion to fulfil my requirement.

Merge array of hash with same key

I have an array of hash as shown here. I want to merge the values of some fields with custom seprators. Here, i show only two hashes in the array, it is possible to have more. But, they are always in same sequence as shown here.
{
"details": [
{
"place": "abc",
"group": 3,
"year": 2006,
"id": 1304,
"street": "xyz 14",
"lf_number": "0118",
"code": 4433,
"name": "abc coorperation",
"group2": 3817,
"group1": 32,
"postal_code": "22926",
"status": 2
},
{
"place": "cbc",
"group": 2,
"year": 2007,
"id": 4983,
"street": "mnc 14",
"lf_number": "0145",
"code": 4433,
"name": "abc coorperation",
"group2": 3817,
"group1": 32,
"postalcode": "22926",
"status": 2
}
],
"#timestamp": "2017-09-04",
"parent": {
"child": [
{
"w_2": 0.5,
"w_1": 0.1,
"id": 14226,
"name": "air"
},
{
"w_2": null,
"w_1": 91,
"id": 25002,
"name": "Water"
}]
},
"p_name": "anacin",
"#version": "1",
"id": 28841
}
I want to edit the details. I want to construct new fields.
Field 1) coorperations: (details.name | details.postal_code details.street ; details.name | details.postal_code details.street)
Output:
Coorperations: (abc coorperation |22926 xyz 14; abc coorperation | 22926 mnc 14)
Field 2) access_code: (details.status-details.id-details.group1-details.group2-details.group(always two digit)/details.year(only last two digits); details.status-details.id-details.group1-details.group2-details.group(always two digit)/details.year(only last two digits))
Output: access_code (2-32-3817-03-06; 2-32-3817-02-07)
How can I achieve this for all the values in details. Here is how final results should look like.
{
"#timestamp": "2017-09-04",
"parent": {
"child": [
{
"w_2": 0.5,
"w_1": 0.1,
"id": 14226,
"name": "air"
},
{
"w_2": null,
"w_1": 91,
"id": 25002,
"name": "Water"
}]
},
"p_name": "anacin",
"#version": "1",
"id": 28841,
"Coorperations" : "abc coorperation |22926 xyz 14; abc coorperation | 22926 mnc 14",
"access_code" : "2-32-3817-03-06; 2-32-3817-02-07"
}
You can try to run this code in rails console with hash is your json:
new_hash = hash.except(:details)
coorperations = ""
access_code = ""
elements = hash[:details]
elements.each do |element|
coorperations = "#{coorperations}#{if coorperations.present? then '; ' else '' end}#{element[:name]} | #{element[:postal_code]} #{element[:street]}"
access_code = "#{access_code}#{if access_code.present? then '; ' else '' end}#{element[:status]}-#{element[:id]}-#{element[:group1]}-#{element[:group2]}-#{element[:group1]}-#{element[:group]}"
end
new_hash.merge!(Coorperations: coorperations)
new_hash.merge!(access_code: access_code)
new_hash

Resources