Search code examples
laravelcollectionslaravel-collection

How to make Laravel Collection of Object instead of Array


How to make Laravel Collection of Object instead of Array ?

If I do like this :

$result = collect([
    'foo' => [
        'success' => 0,
        'failed' => 0
    ],
    'bar' => [
        'success' => 0,
        'failed' => 0
    ]
]);

I will get new instance of laravel collection of array. But, How to make collection of object instead of array ?


Solution

  • Use This:

    $result = json_decode(
        collect([
            'foo' => [
                'success' => 0,
                'failed' => 0
            ],
            'bar' => [
                'success' => 0,
                'failed' => 0
            ]
        ])->toJson()
    );