为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
冉冉说 2023-09-22 17:05:46
我需要从 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<?phpnamespace 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),            ];        });    }}
查看完整描述

1 回答

?
繁华开满天机

TA贡献1816条经验 获得超4个赞

AppProvider.php或类似中添加以下内容。这将禁用 ItemCollections 的数据包装。我希望您重新考虑一下,如果您需要在响应中使用分页或元属性,那么如果没有数据包装,您就没有地方可以放置它们,这就是使用它的原因之一。

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


查看完整回答
反对 回复 2023-09-22
  • 1 回答
  • 0 关注
  • 70 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信