In this post, you will learn how to use toArray() function in laravel collection, Laravel provides toArray() function that will convert the collection into simplifying array. Response data convert into a simple array.
The toArray() method that converts massive collection into a plain simple array, if the collection value is an eloquent model, then the model will also be converted to an array.
Example : toArray() | Laravel Collection Example
Here below working example that will help you to understand the array conversion from the collection.
//EXAMPLE 1
$collection = collect([
'name' => 'jone','age' => 15
])
->toArray();
//EXAMPLE 1 OUTPUT
array:2 [▼
"name" => "jone"
"age" => 15
]
// ---------------------------------------------------
//EXAMPLE 2
$collection = collect([
1,2,3,4
])
->toArray();
//EXAMPLE 2 OUTPUT
array:4 [▼
0 => 1
1 => 2
2 => 3
3 => 4
]
// ---------------------------------------------------
//EXAMPLE 3
$collection = collect([
collect([1,2,3,4,5]),
collect([1,2,3,4,5]),
collect([1,2,3,4,5])
])
->toArray();
//EXAMPLE 3 OUTPUT
array:3 [▼
0 => array:5 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
]
1 => array:5 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
]
2 => array:5 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
]
]
// ---------------------------------------------------
//EXAMPLE 4
$collection = collect([
collect([1,2,3,
collect([1,2,3])
]),
collect([1,2,3,4,5]),
collect([1,2,3,4,5])
])
->toArray();
//EXAMPLE 4 OUTPUT
array:3 [▼
0 => array:4 [▼
0 => 1
1 => 2
2 => 3
3 => array:3 [▼
0 => 1
1 => 2
2 => 3
]
]
1 => array:5 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
]
2 => array:5 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
]
]
Above example will help you to convert your collection into php simple array.
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Laravel 6 - QR Code Generator Example
- How To Generate A Sitemap In Laravel 6/7/8/9
- How To Get Last Record From The Laravel Collection Using last() Collection Method Possible all Example
- Where Clause In Laravel Eloquent Example
- How to return laravel collection in json format - toJson() Method - Laravel Collection
- Median() Method | How It is Different From avg() Method In Laravel Collection
- Laravel Country State City Dropdown Using AJAX
- The GET method is not supported for this route. Supported methods: PATCH
- Laravel 12 REST API Tutorial for Beginners (Step-by-Step Guide 2026)
- Arr::pluck() | Laravel Helper Function