Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

373
Vistas
How to convert array and sub array to collection in Laravel?

I want to convert all of my static data to collection in Laravel.

This is my data:

static $menu_list = [
    [
        'path' => 'admin/report/transaction',
        'active' => 'admin/report',
        'name' => 'Report',
        'icon' => 'file-text',
        'children' => [
            'path' => 'admin/report/transaction',
            'active' => 'admin/report/transaction',
            'name' => 'Transaction',
        ],
    ],
];

This function converts my data to array:

public static function menuList()
{
    $menu_list = collect(self::$menu_list)->map(function ($voucher) {
        return (object) $voucher;
    });
}

but function above can only convert main of array, it can't convert children => [...] to collection.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You need a recursive call.

public static function convertToCollection()
{
    $menu_list = self::menuList(self::$menu_list);
}

public static function menuList($list)
{
    return collect($list)->map(function ($voucher) {
        if(is_array($voucher)) {
          return self::menuList($voucher)
        }
        return $voucher;
    });
}
over 4 years ago · Santiago Trujillo Denunciar

0

You need to use collect() inside map() again:

public static function menuList()
{
    $menu_list = collect(self::$menu_list)->map(function ($voucher) {
        return (object) array_merge($voucher, [
            'children' => collect($voucher['children'])
        ]);
    });
}
over 4 years ago · Santiago Trujillo Denunciar

0

Just add a small code peace to your approach.

  $menu_list = collect(self::$menu_list)->map(function ($voucher) {
             $voucher['children'] = (object) $voucher['children'];
            return (object) $voucher;
        });

Output

   Illuminate\Support\Collection {#574 ▼
  #items: array:1 [▼
    0 => {#573 ▼
      +"path": "admin/report/transaction"
      +"active": "admin/report"
      +"name": "Report"
      +"icon": "file-text"
      +"children": {#567 ▼
        +"path": "admin/report/transaction"
        +"active": "admin/report/transaction"
        +"name": "Transaction"
      }
    }
  ]
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda