删除 Laravel 集合的关键“数据”

我需要从 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);

我的物品收藏.php


<?php


namespace App\Http\Resources;


use Illuminate\Http\Resources\Json\ResourceCollection;

use Illuminate\Support\Facades\Storage;


class ItemCollection extends ResourceCollection

{

    /**

     * Transform the resource collection into an array.

     *

     * @param  \Illuminate\Http\Request  $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),


            ];

        });

    }

}


冉冉说
浏览 73回答 1
1回答

繁华开满天机

在AppProvider.php或类似中添加以下内容。这将禁用 ItemCollections 的数据包装。我希望您重新考虑一下,如果您需要在响应中使用分页或元属性,那么如果没有数据包装,您就没有地方可以放置它们,这就是使用它的原因之一。public&nbsp;function&nbsp;boot(){ &nbsp;&nbsp;&nbsp;&nbsp;ItemCollection::withoutWrapping(); }
打开App,查看更多内容
随时随地看视频慕课网APP