[英]Laravel Collection in Json resource
我對 Laravel 資源有一些問題。 我有 json object 它有一些關系,它看起來像這樣。
{
"product": {
"id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"sku": "2014",
"slug": "container200",
"type": "accessory",
"deleted_at": null,
"created_at": "2021-04-21T08:19:27.000000Z",
"updated_at": "2021-04-21T08:19:27.000000Z",
"category": [],
"info": [
{
"id": "0cb10e21-1772-49fc-bff3-90f30cf899c3",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "375759b3-7aaa-4826-a767-7c071ebe87c0",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "375759b3-7aaa-4826-a767-7c071ebe87c0",
"name": "Italy",
"iso_alpha_2": "IT",
"iso_alpha_3": "ITA",
"native_name": "Italien",
"phone_code": 39,
"capital": "Rome",
"continent": "Europe",
"languages": "{\"bar\":\"Austro-Bavarian German\",\"ita\":\"Italian\",\"srd\":\"Sardinian\"}",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}} {{region_short}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/italy/flag-400.png",
"time_zone": "[\"Europe\\/Rome\"]",
"locales": null,
"isActive": null,
"currency_id": "00cb7204-fb53-4cb7-8f59-ea3fe824c803",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:26.000000Z",
"deleted_at": null
}
},
{
"id": "0d5794fc-0934-4bef-bddc-c3d42ac59d12",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "98e13d4f-2ba3-49f5-a884-800f8158b4c8",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "98e13d4f-2ba3-49f5-a884-800f8158b4c8",
"name": "Slovenia",
"iso_alpha_2": "SI",
"iso_alpha_3": "SVN",
"native_name": "Slovenija",
"phone_code": 386,
"capital": "Ljubljana",
"continent": "Europe",
"languages": "{\"slv\":\"Slovene\"}",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/slovenia/flag-400.png",
"time_zone": "[\"Europe\\/Ljubljana\"]",
"locales": null,
"isActive": null,
"currency_id": "00cb7204-fb53-4cb7-8f59-ea3fe824c803",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:27.000000Z",
"deleted_at": null
}
},
{
"id": "0eaebeb9-1108-4ba8-afc1-1f4e72652631",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "bea0c034-2e18-441b-b01c-08aeddb3018b",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "bea0c034-2e18-441b-b01c-08aeddb3018b",
"name": "Portugal",
"iso_alpha_2": "PT",
"iso_alpha_3": "PRT",
"native_name": "Portugal",
"phone_code": 351,
"capital": "Lisbon",
"continent": "Europe",
"languages": "{\"por\":\"Portuguese\"}",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}} {{region_short}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/portugal/flag-400.png",
"time_zone": "[\"Atlantic\\/Azores\",\"Atlantic\\/Madeira\",\"Europe\\/Lisbon\"]",
"locales": null,
"isActive": null,
"currency_id": "00cb7204-fb53-4cb7-8f59-ea3fe824c803",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:27.000000Z",
"deleted_at": null
}
},
{
"id": "13f53b73-bea0-4e89-be4b-fdca360bcaa9",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "5accef6d-b4c9-4867-b369-77a5c472708e",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "5accef6d-b4c9-4867-b369-77a5c472708e",
"name": "Slovakia",
"iso_alpha_2": "SK",
"iso_alpha_3": "SVK",
"native_name": "Slovensko",
"phone_code": 421,
"capital": "Bratislava",
"continent": "Europe",
"languages": "{\"slk\":\"Slovak\"}",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/slovakia/flag-400.png",
"time_zone": "[\"Europe\\/Bratislava\"]",
"locales": null,
"isActive": null,
"currency_id": "00cb7204-fb53-4cb7-8f59-ea3fe824c803",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:26.000000Z",
"deleted_at": null
}
},
{
"id": "1d571e6a-8da0-42de-8cc9-460a297b2f63",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "8d7c794c-468d-4759-abc9-04f6ad0137ed",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "8d7c794c-468d-4759-abc9-04f6ad0137ed",
"name": "Poland",
"iso_alpha_2": "PL",
"iso_alpha_3": "POL",
"native_name": "Polska",
"phone_code": 48,
"capital": "Warsaw",
"continent": "Europe",
"languages": "{\"pol\":\"Polish\"}",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{region}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/poland/flag-400.png",
"time_zone": "[\"Europe\\/Warsaw\"]",
"locales": null,
"isActive": null,
"currency_id": "27cc3885-756c-4f84-a6ac-69421a9e3bdb",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:27.000000Z",
"deleted_at": null
}
},
{
"id": "2ed66c79-ff2e-4999-baf1-4c73d1bf7d94",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "e5e60f31-d3f8-4d7e-a79c-1c2cb69343a1",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "e5e60f31-d3f8-4d7e-a79c-1c2cb69343a1",
"name": "Spain",
"iso_alpha_2": "ES",
"iso_alpha_3": "ESP",
"native_name": "Espanya",
"phone_code": 34,
"capital": "Madrid",
"continent": "Europe",
"languages": "{\"cat\":\"Catalan\",\"eus\":\"Basque\",\"glg\":\"Galician\",\"oci\":\"Occitan\",\"spa\":\"Spanish\"}",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{region}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/spain/flag-400.png",
"time_zone": "[\"Africa\\/Ceuta\",\"Atlantic\\/Canary\",\"Europe\\/Madrid\"]",
"locales": null,
"isActive": null,
"currency_id": "00cb7204-fb53-4cb7-8f59-ea3fe824c803",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:27.000000Z",
"deleted_at": null
}
},
{
"id": "3432938c-07fe-4405-a8c6-2e6989639e9b",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "20b59d05-e320-4b4c-baf6-d4eede957858",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "20b59d05-e320-4b4c-baf6-d4eede957858",
"name": "Romania",
"iso_alpha_2": "RO",
"iso_alpha_3": "ROU",
"native_name": "România",
"phone_code": 40,
"capital": "Bucharest",
"continent": "Europe",
"languages": "{\"ron\":\"Romanian\"}",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/romania/flag-400.png",
"time_zone": "[\"Europe\\/Bucharest\"]",
"locales": null,
"isActive": null,
"currency_id": "03ff705a-a0a4-49bb-879b-da3ee7f4f396",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:26.000000Z",
"deleted_at": null
}
},
{
"id": "38fd49fb-6aa5-4965-b70a-81fce329d2a1",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "0ae18d7b-3d51-46d4-a577-e3802127d3f0",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "0ae18d7b-3d51-46d4-a577-e3802127d3f0",
"name": "Hungary",
"iso_alpha_2": "HU",
"iso_alpha_3": "HUN",
"native_name": "Magyarország",
"phone_code": 36,
"capital": "Budapest",
"continent": "Europe",
"languages": "{\"hun\":\"Hungarian\"}",
"address_format": "{{recipient}}\n{{city}}\n{{street}}\n{{postalcode}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/hungary/flag-400.png",
"time_zone": "[\"Europe\\/Budapest\"]",
"locales": null,
"isActive": null,
"currency_id": "d6481be9-be00-4cd9-a159-62e7330de03a",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:26.000000Z",
"deleted_at": null
}
},
{
"id": "3c07fe0f-4989-4023-add7-ae20f83580c6",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "a5b579d8-044c-48d0-8134-2aef3e8ee5c9",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "a5b579d8-044c-48d0-8134-2aef3e8ee5c9",
"name": "Lithuania",
"iso_alpha_2": "LT",
"iso_alpha_3": "LTU",
"native_name": "Lietuva",
"phone_code": 370,
"capital": "Vilnius",
"continent": "Europe",
"languages": "{\"lit\":\"Lithuanian\"}",
"address_format": "{{recipient}} {{street}} {{postalcode}} {{city}} {{region}} {{country}}",
"flag": "https://cdn.countryflags.com/thumbs/lithuania/flag-400.png",
"time_zone": "[\"Europe\\/Vilnius\"]",
"locales": null,
"isActive": null,
"currency_id": "00cb7204-fb53-4cb7-8f59-ea3fe824c803",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:27.000000Z",
"deleted_at": null
}
},
{
"id": "3de71c3e-789b-47e5-8b09-1cfa1fd6a776",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "85a30728-8358-4389-b580-d8e52798e870",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "85a30728-8358-4389-b580-d8e52798e870",
"name": "Bulgaria",
"iso_alpha_2": "BG",
"iso_alpha_3": "BGR",
"native_name": "България",
"phone_code": 359,
"capital": "Sofia",
"continent": "Europe",
"languages": "{\"bul\":\"Bulgarian\"}",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/bulgaria/flag-400.png",
"time_zone": "[\"Europe\\/Sofia\"]",
"locales": null,
"isActive": null,
"currency_id": "277d07c8-bf67-4e74-a34e-2f33933cb448",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:27.000000Z",
"deleted_at": null
}
},
{
"id": "442d8f81-f77e-4f05-af2b-28c7bd5a9bdf",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "dd1e92ec-911b-4db0-9922-e9955b6f12f4",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "dd1e92ec-911b-4db0-9922-e9955b6f12f4",
"name": "Estonia",
"iso_alpha_2": "EE",
"iso_alpha_3": "EST",
"native_name": "Eesti",
"phone_code": 372,
"capital": "Tallinn",
"continent": "Europe",
"languages": "{\"est\":\"Estonian\"}",
"address_format": "{{recipient}} {{street}} {{postalcode}} {{city}} {{region}} {{country}}",
"flag": "https://cdn.countryflags.com/thumbs/estonia/flag-400.png",
"time_zone": "[\"Europe\\/Tallinn\"]",
"locales": null,
"isActive": null,
"currency_id": "00cb7204-fb53-4cb7-8f59-ea3fe824c803",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:27.000000Z",
"deleted_at": null
}
},
{
"id": "5999dd11-2786-48c4-bf82-4d378c3421c7",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "231037e7-f30e-47d8-9615-ddb81b9e2cb9",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "231037e7-f30e-47d8-9615-ddb81b9e2cb9",
"name": "Croatia",
"iso_alpha_2": "HR",
"iso_alpha_3": "HRV",
"native_name": "Hrvatska",
"phone_code": 385,
"capital": "Zagreb",
"continent": "Europe",
"languages": "{\"hrv\":\"Croatian\"}",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/croatia/flag-400.png",
"time_zone": "[\"Europe\\/Zagreb\"]",
"locales": null,
"isActive": null,
"currency_id": "553dc2ad-b772-4dbf-9432-107d5733099a",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:26.000000Z",
"deleted_at": null
}
},
{
"id": "65c9c254-0ae8-454c-9cd4-e9803fe80395",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "e6c6030b-fdd6-4d52-ab8f-8f4618acf62b",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "e6c6030b-fdd6-4d52-ab8f-8f4618acf62b",
"name": "Germany",
"iso_alpha_2": "DE",
"iso_alpha_3": "DEU",
"native_name": "Deutschland",
"phone_code": 49,
"capital": "Berlin",
"continent": "Europe",
"languages": "{\"deu\":\"German\"}",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/germany/flag-400.png",
"time_zone": "[\"Europe\\/Berlin\",\"Europe\\/Busingen\"]",
"locales": null,
"isActive": null,
"currency_id": "00cb7204-fb53-4cb7-8f59-ea3fe824c803",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:27.000000Z",
"deleted_at": null
}
},
{
"id": "94abdee5-5853-41d1-b8f8-8044308483cf",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "7156e359-f00b-4da5-9083-58e9b0b401ce",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "7156e359-f00b-4da5-9083-58e9b0b401ce",
"name": "Czech Republic",
"iso_alpha_2": "CZ",
"iso_alpha_3": "CZE",
"native_name": "Česká republika",
"phone_code": 420,
"capital": "Prague",
"continent": "Europe",
"languages": "{\"ces\":\"Czech\",\"slk\":\"Slovak\"}",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/czech-republic/flag-400.png",
"time_zone": "[\"Europe\\/Prague\"]",
"locales": null,
"isActive": null,
"currency_id": "4c463089-f1a8-4efb-a063-100877c2f8ef",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:27.000000Z",
"deleted_at": null
}
},
{
"id": "be6242a3-7b83-47d4-91d4-284ab2183ee9",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "11e0a165-1148-45db-82df-0fd9f012e0d3",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "11e0a165-1148-45db-82df-0fd9f012e0d3",
"name": "Latvia",
"iso_alpha_2": "LV",
"iso_alpha_3": "LVA",
"native_name": "Latvija",
"phone_code": 371,
"capital": "Riga",
"continent": "Europe",
"languages": "{\"lav\":\"Latvian\"}",
"address_format": "{{recipient}} {{street}} {{postalcode}} {{city}} {{region}} {{country}}",
"flag": "https://cdn.countryflags.com/thumbs/latvia/flag-400.png",
"time_zone": "[\"Europe\\/Riga\"]",
"locales": null,
"isActive": null,
"currency_id": "00cb7204-fb53-4cb7-8f59-ea3fe824c803",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:26.000000Z",
"deleted_at": null
}
},
{
"id": "ee1fe9ee-9331-4459-82a5-4ce54a713c2f",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"country_id": "39399fb1-0400-487a-ae36-a9a98bf5780f",
"name": "Finer Dicer Container 200ml",
"description": "#",
"created_at": "2021-04-21T08:19:28.000000Z",
"updated_at": "2021-04-21T08:19:28.000000Z",
"deleted_at": null,
"country": {
"id": "39399fb1-0400-487a-ae36-a9a98bf5780f",
"name": "Greece",
"iso_alpha_2": "GR",
"iso_alpha_3": "GRC",
"native_name": "Ελλάδα",
"phone_code": 30,
"capital": "Athens",
"continent": "Europe",
"languages": "{\"ell\":\"Greek\"}",
"address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{country}}",
"flag": "https://cdn.countryflags.com/thumbs/greece/flag-400.png",
"time_zone": "[\"Europe\\/Athens\"]",
"locales": null,
"isActive": null,
"currency_id": "00cb7204-fb53-4cb7-8f59-ea3fe824c803",
"created_at": "2021-04-21T08:19:26.000000Z",
"updated_at": "2021-04-21T08:19:26.000000Z",
"deleted_at": null
}
}
],
"images": [
{
"id": "a59b174e-5d73-4055-825b-99d7ee814fa9",
"product_id": "087fd1dd-d4e2-4a1a-8f62-0481203e5afd",
"order": null,
"name": "container200_feature_image",
"image_url": "https://globaxy.com/api/gmanager/accessories/nicer-dicer-200ml-box.jpg",
"image_path": null,
"created_at": "2021-04-21T08:19:32.000000Z",
"updated_at": "2021-04-21T08:19:32.000000Z",
"deleted_at": null
}
],
"packages": []
}
}
所以我創建了如下所示的資源:
public function toArray($request)
{
return [
'id' => $this->id,
'sku' => $this->sku,
'type' => $this->type,
'info' => InfoCollection::collection($this->info)
];
//return parent::toArray($request);
}
所以現在,問題出在 InfoCollection 上,我想傳遞這些信息並顯示它們,但是每次我嘗試獲取 id 時,它都會說
未定義屬性:Illuminate\Database\Query\Builder::$id
但它肯定是 foreaching 值,因為我嘗試過:
class InfoCollection extends ResourceCollection
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return ([
'id' => 'hereis id',
]);
}
}
它是foreaching,但我無法獲得任何價值。
任何想法如何解決這個問題?
你是如何嘗試獲取ID的。 你不應該這樣做: $this->info->id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.