Add a custom attribute to a Laravel / Eloquent model on load?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Extending Laravel/Eloquent Models with Custom Attributes on Load
Introduction: Working with complex database models in Laravel often requires handling additional information that is not stored directly in the underlying table. One approach to achieving this is by utilizing custom attributes or properties within your model that are populated at load time, rather than through looping and manual calculations. This blog post aims to provide a comprehensive solution for adding such custom attributes to Laravel/Eloquent models upon loading.
1. Create a Custom Accessor Method:
Firstly, define an accessor method in your model that will return the calculated value for the 'available' attribute you want to add dynamically. This allows you to use the model's native functionality and keeps your code more organized. Here's an example for EventSession model:
```php
class EventSession extends Model {
public function getAvailableAttribute() {
return $this->getAvailability();
}
}
```
This method will set the 'available' attribute when loading the model and also allows you to access it directly through the model instance:
```php
$session = EventSession::find(1);
echo $session->available;
```
2. Using Model Events for Custom Attributes:
In case your use-case requires more complex calculations or if you want to set a custom attribute before any other attributes are loaded, it would be beneficial to make use of Laravel's model events. You can define an event listener class for 'creating', 'updating', and 'retrieving' the EventSession models to populate your custom attribute:
```php
class LoadCustomAttributeEvent implements ShouldQueue {
public function handle(Model $model, Closure $next) {
if ($model instanceof EventSession) {
// Your custom logic for setting availability goes here.
$model->setAvailability();
}
return $next($model);
}
}
```
You can then register your event listener in the 'EventServiceProvider':
```php
class EventServiceProvider extends ServiceProvider {
public function boot() {
LoadCustomAttributeEvent::listen(['creating', 'updating', 'retrieving']);
}
}
```
By leveraging model events, you can ensure that your custom attribute is always populated and consistent across all operations.
3. Handling Complex Calculations:
If you need to perform more complex calculations, such as checking availability based on multiple conditions or database queries, consider using a helper function within your EventSession model. Here's an example of how you could handle this scenario:
```php
class EventSession extends Model {
public static function calculateAvailability($session) {
$isAvailable = true; // Initial assumption of availability
$conditions = [// Conditions for availability check];
foreach ($conditions as $condition) {
if (!$session->checkCondition($condition)) {
$isAvailable = false;
break;
}
}
return $isAvailable;
}
}
```
Now, you can call this helper function in your model event listener:
```php
class LoadCustomAttributeEvent implements ShouldQueue {
public function handle(Model $model, Closure $next) {
if ($model instanceof EventSession) {
// Set availability based on conditions.
$model->availability = self::calculateAvailability($model);
}
return $next($model);
}
}
```
Conclusion: Adding custom attributes to Laravel/Eloquent models at load time is a valuable technique for handling complex calculations and ensuring data consistency. By utilizing accessor methods, model events, and helper functions, you can extend your framework's functionality while maintaining its simplicity and elegance. For more in-depth explanations and examples on this topic, be sure to check out the Laravel Company blog: https://laravelcompany.com/blog/.