How to get keys name from array in laravel

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: How to Retrieve Array Keys as Names in Laravel When working with models and their data, you may want to get the keys' names of a returned array. In this blog post, we'll discuss how to retrieve these names from an array generated by using `Model::all()` in Laravel. Let's dive into the process step-by-step. 1. Understanding the default response: When you call `Model::all()`, it returns an Eloquent collection, which is a PHP object that contains a set of model instances for the specified table. By default, this method provides information about each instance in the array, such as their attributes, relationships, and other properties. However, it does not include keys' names, making it difficult to distinguish between these objects. 2. Getting column and table names: To get the column names of a model's table, you can use the `$table->getColumnList()` method. For instance, if we have a User model with columns like 'id', 'first_name', 'last_name', etc., calling `User::select('id', 'first_name', 'last_name')->get()->toArray()`, you'll get an array containing the values of these specific columns. 3. Retrieving keys as names: If you need to create a list or display the column names with their respective values, you can use the following technique for retrieving the array keys as names. Here's an example:
$users = User::all(); // Collection Object
$keys_as_names = [];
foreach ($users as $user) {
    foreach ($user->getAttributes() as $key => $value) {
        if (!in_array($key, $keys_as_names)) {
            $keys_as_names[] = $key;
        }
    }
}
In this example, we first get all users using `User::all()` to obtain a collection. Then, within a loop through each user, we iterate over the attributes of that user by accessing their `getAttributes()`. We store the keys found in an array called `$keys_as_names`, ensuring they are not duplicated (we use `in_array()`) and are added as names. 4. Making it more efficient: The previous approach iterates over every user and attribute, which becomes less efficient on large collections. To optimize this process, you can use Laravel's collection methods to do the same task in a single line of code:
$keys_as_names = collect(Model::all())->flatten(1)->unique()->values();
This code converts the Eloquent collection into a flat array, removes duplicates using `unique()`, and then gets the values using `values()`. This way, you can easily access and manipulate the keys as names. 5. Conclusion: In this blog post, we learned how to get the column names from an array generated by `Model::all()` in Laravel. By understanding the default response and using different techniques to optimize the process, we can efficiently retrieve the keys' names for our use cases. Remember to always follow best practices and keep your code clean and efficient. For more information on working with collections and models in Laravel, visit https://laravelcompany.com/blog/.