Laravel Tutorials

Performance and Value Objects in Laravel Accessors

Published
Performance and Value Objects in Laravel Accessors image

Laravel's Eloquent ORM enhances accessor functionality with built-in caching and value object support. These features enable efficient handling of complex computations and structured data while maintaining clean, maintainable code.

This approach proves particularly valuable when working with computationally expensive operations or when you need to represent complex data structures as proper objects rather than plain arrays.

protected function complexStats(): Attribute
{
return Attribute::make(
get: fn () => $this->calculateStats()
)->shouldCache();
}

Here's an example implementing location handling with value objects:

<?php
 
namespace App\Models;
 
use App\ValueObjects\Location;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
 
class Store extends Model
{
protected function location(): Attribute
{
return Attribute::make(
get: fn ($value) => new Location(
latitude: $this->latitude,
longitude: $this->longitude,
address: $this->address,
timezone: $this->timezone
),
set: function (Location $location) {
return [
'latitude' => $location->latitude,
'longitude' => $location->longitude,
'address' => $location->address,
'timezone' => $location->timezone
];
}
)->shouldCache();
}
 
protected function operatingHours(): Attribute
{
return Attribute::make(
get: fn () => $this->calculateHours()
)->withoutObjectCaching();
}
 
private function calculateHours()
{
// Dynamic calculation based on timezone and current time
return $this->location->getLocalHours();
}
}
$store = Store::find(1);
$store->location->address = '123 New Street';
$store->save();
 
// Access operating hours (recalculated each time)
$hours = $store->operatingHours;

Laravel's accessor features provide powerful tools for handling complex data structures and optimizing performance through intelligent caching.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article