Get all fields from db model in Laravel

Get all fields from db model in Laravel

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

Stefan Bogdanescu

Founder & Senior Architect

Retrieving All Model Attributes, Including Empty Columns in Laravel

When you retrieve a model instance from the database using Eloquent, the framework defaults to returning only the columns that actually hold data. If your capacity column is null or empty in the database, calling $model->attributesToArray() will omit it entirely from the returned array. This behavior stems from how Eloquent maps database records to PHP objects; it essentially filters out "empty" values during the hydration process unless explicitly told otherwise.

Consider a scenario where you are building a shop management interface. Your Drink model has an id, shopID, name, and bottleID. You also have a capacity column intended to store volume measurements, but for some drinks (like bottled water or specific cocktails), this value might be intentionally left blank. When you query the database, you get a row where capacity is effectively empty:

----------------------------------------------
DB: | id | shopID | name  | bottleID | capacity |
----------------------------------------------
| 1  | 8      | Cola  |   3      | NULL     |
----------------------------------------------

If you simply call $drink->attributesToArray() on this instance, the resulting array might look like ['id' => 1, 'name' => 'Cola', 'bottleID' => 3]. The capacity key is missing. If your view or controller logic expects every possible column defined in the migration to exist in the attribute array—even if the value is an empty string or null—this default behavior breaks your application flow.

To fix this, you cannot rely solely on the automatic hydration provided by Eloquent. You must explicitly define the columns you want to include. The most robust way to ensure all attributes are present, regardless of their current values, is to use the hidden and appends properties or to manually merge the schema columns into the attribute array. However, a simpler approach for specific views is to access the underlying raw attributes directly or define the model's $fillable array carefully while ensuring you don't accidentally filter them out during serialization.

If you need the empty fields to appear as an empty string rather than null (which helps when passing data to Blade templates where {{ $drink->capacity }} should render nothing but not break layout), you can override the accessor or simply force the inclusion of all columns via the model's $attributes property before converting it.

Here is a practical pattern to ensure every column defined in your migration is present in the array, including those with null values:

public function getDrinkData(Request $request)
{
    $drink = Drink::where('shopId', $request->session()->get('shopId'))->first();

    if ($drink) {
        // Get all attributes, but we might need to ensure specific columns are not filtered out
        // depending on your model's fillable configuration or custom casts.
        $attributes = array_merge($drink->attributesToArray(), $this->getEmptyColumns());
        
        // Alternatively, simply iterate through the schema and set defaults if missing
        foreach ($this->getSchemaColumns() as $column) {
            if (!array_key_exists($column, $attributes)) {
                $attributes[$column] = ''; 
            }
        }
        
        $drink = $attributes;
    } else {
        $drink = Drink::firstOrNew(['shopId' => $request->session()->get('shopId')]);
        // Same logic applies here if you need to initialize with empty values
    }

    return view('shop.drink')->compact(['drink' => $drink]);
}

A cleaner, more idiomatic solution involves defining a method within the Drink model itself that exposes all columns as attributes. You can create a relationship or simply define an array of expected columns and merge them into the instance:

// In your Drink Model
public function toArray($options = [])
{
    $attributes = parent::toArray($options);
    
    // List all columns you expect, including capacity
    $expectedColumns = ['id', 'shopID', 'name', 'bottleID', 'capacity'];
    
    foreach ($expectedColumns as $column) {
        if (!array_key_exists($column, $attributes)) {
            $attributes[$column] = ''; 
        }
    }
    
    return $attributes;
}

By overriding the toArray method, you ensure that whenever you serialize the model, it includes every column defined in your schema. This approach is flexible and avoids hardcoding values in controllers. It also respects Laravel's casting rules unless you explicitly override them. If capacity is nullable in the database, accessing $drink->capacity will naturally return null. If you strictly require an empty string for view rendering, you can cast that specific attribute to a string in the model definition or within the overridden method.

This pattern ensures your views receive a consistent data structure, preventing layout shifts or undefined index warnings when optional fields are missing from the database record. It is a standard practice when dealing with dynamic forms or API responses where schema consistency is paramount.

Our network