Get Laravel Models with All Attributes

Get Laravel Models with All Attributes

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

Stefan Bogdanescu

Founder & Senior Architect

Get Laravel Models with All Attributes

When querying a model in Laravel, you might encounter a situation where only non-null attributes are returned by default. This often happens because Eloquent uses toArray() or similar methods that filter out null values to keep the payload clean. However, if your application logic relies on distinguishing between an explicitly set null and a missing property, this default behavior becomes a significant obstacle.

Consider a scenario where you have a single array of attributes intended to update two different models. Your update function iterates through this array, checking property_exists() to determine whether the model possesses the specific attribute before assigning a value. The logic assumes that if the key exists in the input array and the property exists on the model instance, the assignment should proceed.

The problem arises when an attribute is currently null on the model but not present in the database row or the current serialized state being inspected. In many cases, accessing a missing property on a model triggers dynamic attribute casting or accessors that might return null, but strictly speaking, the property itself might not exist if it was never mass-assignable or defined as a fillable field with a default value. More critically, when using property_exists(), you are checking for the physical existence of the accessor or mutator on the class definition. If a column in the database is null and your model uses dynamic casting (like $casts['status' => 'string']), the property exists because Laravel generates it on demand. However, if you are relying on strict property presence without considering that Eloquent models often handle missing mass-assignable keys by simply not setting them in the underlying array representation before hydration completes, property_exists() can return false for attributes that should be updatable.

The core issue is the distinction between a property existing on the class and having a value associated with it in the current instance state. If your update function skips an attribute because property_exists() returns false, you effectively ignore updates for fields that were previously null but are now expected to be set. This breaks the contract of applying a unified dataset across multiple model instances.

To resolve this, you should stop relying on property_exists() to gatekeep property assignments. Instead, leverage Laravel's mass assignment protections and the $attributes array directly after hydration. When you retrieve a model from the database, Eloquent ensures that all fillable attributes are available in the $attributes key, even if their values are null.

A more robust approach involves bypassing the existence check entirely and trusting the input array structure. Since you expect the array keys to match the model's fillable columns, you can iterate through the array and assign values directly:

foreach ($inputArray as $key => $value) {
    // Ensure the key is a valid fillable attribute to prevent mass assignment vulnerabilities
    if (in_array($key, $model->getFillable(), true)) {
        $model->setAttribute($key, $value);
    }
}

By checking getFillable() instead of property_exists(), you align your logic with Laravel's security model and the actual schema definition. This ensures that even if a database column contains null, or if the property is accessed via dynamic casting, the assignment proceeds correctly. The setAttribute method handles the hydration logic, ensuring the value is stored in the internal state regardless of whether it was previously null.

This approach also mitigates the risk of accidentally modifying protected fields or attributes that are not part of your intended update payload. If you need to handle specific cases where a property might be dynamically cast but isn't strictly fillable, ensure your model configuration explicitly defines those casts. This guarantees that property_exists() returns true for the accessor, allowing your original logic to work if you strictly require that check, though relying on getFillable() is generally safer for mass updates.

For further details on managing model attributes and casting in Laravel, resources like https://laravelcompany.com provide comprehensive guides on Eloquent internals and best practices for handling model data structures efficiently.

Our network