How to set a default attribute value for a Laravel / Eloquent model?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Setting Default Attribute Values for Laravel/Eloquent Models Made Easy Introduction: One of the most common issues developers encounter when working with Laravel/Eloquent models is setting default attribute values. This can be a bit tricky, as simply declaring properties in your model class may not have the desired outcome. In this blog post, we will explore how to set the default attribute value for a Laravel/Eloquent model effectively and efficiently. 1. Understanding Attributes vs. Properties: To understand why setting default values directly on model properties does not work, let's first differentiate between attributes and properties in Laravel. Attributes are public properties that represent data stored in the database table. These will be accessible through the getters/setters defined by Eloquent. On the other hand, properties can be private or protected variables used within your model class to organize and manage internal state or functionality. 2. Defining Attributes: To set a default value for an attribute in Laravel/Eloquent models, use the 'attribute' syntax during model definition:
public function setAttribute($key, $value) {
    if (strpos($key, '_') !== 0 && strtolower(substr($key, 0, 1)) != '_') {
        parent::setAttribute($key, $value);
    }
}
In this example, we have defined a method that checks if the attribute name follows convention (starting with an uppercase letter or underscore) before invoking the corresponding parent function. We do this to ensure only valid attributes are set and not any internal data. 3. Setting Default Attribute Values: Now, let's set up our model class with a default value for one of its attributes (quantity). First, define the attribute in your model class:
class Product extends Model {
    protected $attributes = [
        'quantity' => 9,
    ];
}
In this code snippet, we have added the attribute 'quantity' and set its default value to nine. Whenever a new instance of the model is created or retrieved from the database, this default value will be applied. Alternatively, you can use Laravel's built-in methods to perform the same task:
class Product extends Model {
    public function __construct(array $attributes = []) {
        if (!isset($attributes['quantity'])) {
            $attributes['quantity'] = 9;
        }
        parent::__construct($attributes);
    }
}
In this approach, we are overriding the constructor of the model class. We check if the incoming attributes array includes a 'quantity' key and set its value to nine if it isn't present. This ensures that our default value is applied whenever we create or retrieve an instance of the Product model. 4. Conclusion: Setting default attribute values in Laravel/Eloquent models is crucial for maintaining data integrity and ensuring consistency across various queries. By understanding the difference between attributes and properties, you can effectively manage your model's state without compromising its performance or ease-of-use. For more detailed guidance on this topic and other Laravel development best practices, check out https://laravelcompany.com for comprehensive tutorials and articles.