laravel:how to get columns name of eloquent model?

laravel:how to get columns name of eloquent model?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Eloquent Model Column Names in Laravel

When building an edit form for a model like News, you often need to dynamically generate input fields based on the database schema. The code snippet provided attempts to access $curNews->attributes to iterate over the data. While this works to retrieve the values, it is not the most robust way to get the column names themselves, nor does it guarantee consistency with your database structure.

In Laravel 5.2 and later versions, the attributes property on an Eloquent model instance holds the actual attribute values retrieved from the database for that specific record. However, iterating over this array is prone to issues if you are trying to discover the names of the columns without assuming they map perfectly to your PHP class properties or mass-assignment whitelist.

Using the Model's Attributes Property

The most direct approach to get the column names currently held in the model instance is to use the attributes property, but with a slight modification. In Laravel 5.2+, this property returns an array where keys are the attribute names and values are the corresponding data. You can iterate over this array as shown in your source code:

$curNews = News::find($request->newsID);

if (!$curNews) {
    return back()->withErrors(['Unable to Edit News: No Data Found For Given News ID']);
}

foreach ($curNews->attributes as $key => $val) {
    // $key now contains the column name from the database
    $data[$key] = $val;
}

This method works because Eloquent automatically casts the raw data into an array format. However, this approach relies on the fact that the model has already been hydrated. If you are fetching a model that was previously casted to JSON or accessed via $model->toArray(), the behavior might vary slightly depending on Laravel's version and configuration.

The Better Approach: Using getAttributes()

If your goal is strictly to list column names for form generation, using $attributes is functionally identical in older versions but less explicit. A cleaner, more modern way to access these keys—available consistently across Laravel 5.x—is the getAttributes() method. This method returns an associative array containing all attributes of the model instance.

$columns = $curNews->getAttributes();

foreach ($columns as $columnName => $value) {
    // $columnName is your database column name
    $data[$columnName] = $value;
}

Using getAttributes() makes the intent clearer: you are explicitly asking for the attribute mapping. It avoids any potential confusion with the implicit $attributes property, which behaves differently in specific edge cases regarding hidden or fillable attributes depending on the Laravel version.

Handling Hidden and Fillable Attributes

A critical pitfall to consider is that getAttributes() includes all columns from the database, regardless of whether they are defined as $fillable or marked as hidden. If your News model defines a $fillable property to protect against mass assignment vulnerabilities, iterating over all attributes might inadvertently expose sensitive columns you didn't intend to render in the view.

For instance, if your database has a password_hash column but your model does not list it in $fillable, and you are building an edit form for the public or internal admin panel, you should filter these out. You can check against the fillable property like this:

$fillableColumns = collect($curNews->getAttributes())
    ->filter(function ($value, $key) use ($curNews) {
        return in_array($key, $curNews->getFillable(), true);
    })
    ->keys();

This ensures that only the columns intended for mass assignment are passed to the view. This is a best practice when generating dynamic forms, as it prevents accidental exposure of sensitive fields and keeps your form inputs aligned with security boundaries.

Conclusion on Implementation

For Laravel 5.2, using getAttributes() provides a reliable way to retrieve column names while maintaining consistency with how Eloquent handles data retrieval. Always remember to validate the model exists before accessing its attributes. If you are moving to newer Laravel versions (6.0+), similar logic applies, though methods like $model->fillable might be accessed directly for more granular control in complex scenarios.

Our network