删除集合的密钥'data' 拉拉维尔



我需要从Laravel中的集合中删除"data"键。这对我有效,但它删除了我想保留的其他密钥,我只需要删除"数据"密钥:

return $filteredValues ​​= $collection->values ​()->all(); // I remove other keys inside the objects.

我的收藏退货:

$records = Item::where('tienda_id',$id)->where('item.nombre', 'like', "%" . $query . "%")->take(50)->get();
return $collection = new ItemCollection($records);

My ItemCollection.php

<?php
namespace AppHttpResources;
use IlluminateHttpResourcesJsonResourceCollection;
use IlluminateSupportFacadesStorage;
class ItemCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param  IlluminateHttpRequest  $request
* @return mixed
*/
public function toArray($request)
{
return $this->collection->transform(function($row, $key) {
return [
'id' => $row->id,
'nombre' => $row->nombre,
'marca_id' => $row->marca_id,
'tienda_id' => $row->tienda_id,
'nombre_marca' => $row->marca->nombre_marca,
'unidad_id' => $row->unidad_id,
'nombre_unidad' => $row->unidad->nombre_unidad,
'tipo_cambio' => $row->tienda->tipocambio,
'categoria_id' => $row->categoria_id,
'stock' => $row->stock,
'moneda' => $row->moneda,
'codigos' => $row->codigos,
'stockminimo' => $row->stockminimo,
'stockmaximo' => $row->stockmaximo,
'impuesto_id' => $row->impuesto_id,
'primer_margen' => $row->primer_margen,
'segundo_margen' => $row->segundo_margen,
'precio' => $row->precio,
'notas' => $row->notas,
'imagen' => url('images/'.$row->imagen),
];
});
}
}

我的JSON:

{
"data": [
{
"id": 27,
"nombre": "Nombre de prueba",
"marca_id": 2,
"tienda_id": 2,
"nombre_marca": "marca 2",
"unidad_id": 59,
"nombre_unidad": "NIU",
"tipo_cambio": "3.54",
"categoria_id": 1,
"stock": 100,
"moneda": "$",
"codigos": [
{
"id": 2,
"nombre_codigo": "Codigo",
"tienda_id": 2,
"created_at": "2020-08-03T15:19:11.000000Z",
"updated_at": "2020-08-06T22:38:02.000000Z",
"pivot": {
"item_id": 27,
"codigo_id": 2,
"nombre": "66677kj"
}
},
{
"id": 5,
"nombre_codigo": "CODIGO 2",
"tienda_id": 2,
"created_at": "2020-08-07T20:45:29.000000Z",
"updated_at": "2020-08-07T20:45:29.000000Z",
"pivot": {
"item_id": 27,
"codigo_id": 5,
"nombre": "78877k"
}
}
],
"stockminimo": 1,
"stockmaximo": 100,
"impuesto_id": 1,
"primer_margen": "35.00",
"segundo_margen": "20.00",
"precio": "5.07",
"notas": "jkjkkjkkj",
"imagen": "http://maks.test/images/159692202782450637_121718839341269_2075616741920079872_o.jpg"
},
{
"id": 28,
"nombre": "Aleta de pollo",
"marca_id": 1,
"tienda_id": 2,
"nombre_marca": "marca1x",
"unidad_id": 59,
"nombre_unidad": "NIU",
"tipo_cambio": "3.54",
"categoria_id": 1,
"stock": 5,
"moneda": "S/",
"codigos": [
{
"id": 2,
"nombre_codigo": "Codigo",
"tienda_id": 2,
"created_at": "2020-08-03T15:19:11.000000Z",
"updated_at": "2020-08-06T22:38:02.000000Z",
"pivot": {
"item_id": 28,
"codigo_id": 2,
"nombre": "jhjjkj"
}
},
{
"id": 5,
"nombre_codigo": "CODIGO 2",
"tienda_id": 2,
"created_at": "2020-08-07T20:45:29.000000Z",
"updated_at": "2020-08-07T20:45:29.000000Z",
"pivot": {
"item_id": 28,
"codigo_id": 5,
"nombre": "jkk"
}
}
],
"stockminimo": 6,
"stockmaximo": 88,
"impuesto_id": 1,
"primer_margen": "2.00",
"segundo_margen": "10.00",
"precio": "100.00",
"notas": "jkjkkjjk",
"imagen": "http://maks.test/images/159698573182341068_123146022531884_1345097685962588160_o.jpg"
}
]
}

我需要它看起来像这样:

[
{
"id": 27,
"nombre": "Nombre de prueba",
"marca_id": 2,
"tienda_id": 2,
"nombre_marca": "marca 2",
"unidad_id": 59,
"nombre_unidad": "NIU",
"tipo_cambio": "3.54",
"categoria_id": 1,
"stock": 100,
"moneda": "$",
"codigos": [
{
"id": 2,
"nombre_codigo": "Codigo",
"tienda_id": 2,
"created_at": "2020-08-03T15:19:11.000000Z",
"updated_at": "2020-08-06T22:38:02.000000Z",
"pivot": {
"item_id": 27,
"codigo_id": 2,
"nombre": "66677kj"
}
},
{
"id": 5,
"nombre_codigo": "CODIGO 2",
"tienda_id": 2,
"created_at": "2020-08-07T20:45:29.000000Z",
"updated_at": "2020-08-07T20:45:29.000000Z",
"pivot": {
"item_id": 27,
"codigo_id": 5,
"nombre": "78877k"
}
}
],
"stockminimo": 1,
"stockmaximo": 100,
"impuesto_id": 1,
"primer_margen": "35.00",
"segundo_margen": "20.00",
"precio": "5.07",
"notas": "jkjkkjkkj",
"imagen": "http://maks.test/images/159692202782450637_121718839341269_2075616741920079872_o.jpg"
},
{
"id": 28,
"nombre": "Aleta de pollo",
"marca_id": 1,
"tienda_id": 2,
"nombre_marca": "marca1x",
"unidad_id": 59,
"nombre_unidad": "NIU",
"tipo_cambio": "3.54",
"categoria_id": 1,
"stock": 5,
"moneda": "S/",
"codigos": [
{
"id": 2,
"nombre_codigo": "Codigo",
"tienda_id": 2,
"created_at": "2020-08-03T15:19:11.000000Z",
"updated_at": "2020-08-06T22:38:02.000000Z",
"pivot": {
"item_id": 28,
"codigo_id": 2,
"nombre": "jhjjkj"
}
},
{
"id": 5,
"nombre_codigo": "CODIGO 2",
"tienda_id": 2,
"created_at": "2020-08-07T20:45:29.000000Z",
"updated_at": "2020-08-07T20:45:29.000000Z",
"pivot": {
"item_id": 28,
"codigo_id": 5,
"nombre": "jkk"
}
}
],
"stockminimo": 6,
"stockmaximo": 88,
"impuesto_id": 1,
"primer_margen": "2.00",
"segundo_margen": "10.00",
"precio": "100.00",
"notas": "jkjkkjjk",
"imagen": "http://maks.test/images/159698573182341068_123146022531884_1345097685962588160_o.jpg"
}
]

提前感谢阅读我,希望你能帮我解决这个问题,谢谢!

AppProvider.php或类似文件中添加以下内容。这将禁用ItemCollections的数据包装。你我想让你重新考虑一下,如果你需要在你的回复上使用分页或元属性,你没有数据包装就无法将它们放在哪里,这也是使用它的原因之一。

public function boot()
{
ItemCollection::withoutWrapping();
}

相关内容

最新更新