Two Laravel devs that won't disappear on you. Finally! Hire Joel and Aaron from No Compromises.

Typed Objects for Eloquent with Expressive

Last updated on by

Typed Objects for Eloquent with Expressive image

Expressive, by Wendell Adriel, converts Eloquent models into typed PHP objects and can convert those objects back into Eloquent models when your application needs to persist data. The goal is to give services, actions, and tests a typed object boundary while Eloquent continues to handle querying, relationships, casts, visibility rules, mass assignment, and database writes.

Instead of passing full Eloquent models throughout your codebase, you work with lightweight objects that have public, typed properties. This keeps your domain logic separate from the database layer without forcing you to abandon Eloquent.

Opting Models In

Models opt in by adding the IsExpressive trait. Attributes like #[Fillable] and #[Hidden] describe how the model maps to its typed counterpart:

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use WendellAdriel\Expressive\Concerns\IsExpressive;
 
#[Fillable(['name', 'email', 'role', 'password'])]
#[Hidden(['password', 'remember_token'])]
class User extends Authenticatable
{
use IsExpressive;
 
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
 
public function address(): HasOne
{
return $this->hasOne(Address::class);
}
 
protected function displayName(): Attribute
{
return Attribute::make(
get: fn (): string => "{$this->name} ({$this->role->value})",
);
}
}

Generating Expressive Classes

The package includes an Artisan command to scaffold a typed class from an existing model:

php artisan make:expressive User --model="App\Models\User"

By default, generated classes live in App\Expressive. Each class extends the base Expressive class with typed public properties that mirror the model's columns, relationships, and accessors:

use App\Enums\UserRole;
use App\Expressive\Address;
use App\Expressive\Post;
use Carbon\CarbonInterface;
use Illuminate\Support\Collection;
use WendellAdriel\Expressive\Attributes\Relationship;
use WendellAdriel\Expressive\Attributes\Virtual;
use WendellAdriel\Expressive\Expressive;
 
final class User extends Expressive
{
public ?int $id = null;
 
public string $name;
 
public string $email;
 
public UserRole $role;
 
public ?CarbonInterface $createdAt = null;
 
#[Relationship]
public ?Address $address = null;
 
/** @var Collection<int, Post>|null */
#[Relationship]
public ?Collection $posts = null;
 
#[Virtual]
public ?string $displayName = null;
}

The #[Relationship] attribute marks properties that map to Eloquent relations, and #[Virtual] marks accessor-backed values like displayName that are not real columns.

Converting Models to Typed Objects

Call expressive() on a model, collection, or query builder to get back typed objects. You can selectively include accessors and relationships:

// A single model with a virtual attribute
$user = User::findOrFail(1)->expressive(attributes: ['display_name']);
 
// A collection with an eager-loaded relationship
$users = User::query()->get()->expressive(relationships: ['posts']);
 
// Straight from the query builder
$users = User::query()
->where('active', true)
->expressive(relationships: ['posts']);

Loaded relationships are converted recursively. A HasOne relation becomes the related model's Expressive object, and a HasMany relation becomes a Collection of Expressive objects.

Persisting Typed Objects

Expressive can also go the other direction. The model() method builds an unsaved Eloquent instance from the typed object, while save() persists it:

// Build an in-memory model without writing to the database
$model = (new App\Expressive\User([
'name' => 'Wendell',
'email' => 'wendell@example.com',
]))->model();
 
// Persist the model and its supported relationships
$saved = (new App\Expressive\User([
'name' => 'Wendell',
'email' => 'wendell@example.com',
]))->save();

The save() method persists the root model along with supported direct relationships, including BelongsTo, HasOne, HasMany, MorphOne, and MorphMany. Both methods respect Eloquent's mass-assignment rules and ignore non-fillable attributes, so persistence flows through Eloquent's standard mechanisms.

Installation

You can install the package via Composer:

composer require wendelladriel/laravel-expressive

You can then publish the config file:

php artisan vendor:publish --tag="expressive"

The package requires PHP 8.3 or higher and supports Laravel 12 and 13. For more information, you can read the documentation or view the source code on GitHub.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift

The latest

View all →
Malware Blocking and Dependency Policies in Composer 2.10 image

Malware Blocking and Dependency Policies in Composer 2.10

Read article
Aegis for Laravel: Scaffolding and Validation Helpers for Value Objects image

Aegis for Laravel: Scaffolding and Validation Helpers for Value Objects

Read article
Playa: Cookie-Based Temporary Players for Laravel image

Playa: Cookie-Based Temporary Players for Laravel

Read article
Scheduler Attributes and Listener Discovery Control in Laravel 13.12.0 image

Scheduler Attributes and Listener Discovery Control in Laravel 13.12.0

Read article
The PHP Foundation Launches an Ecosystem Security Team image

The PHP Foundation Launches an Ecosystem Security Team

Read article
Manage Subscription Plans and Entitlements in Laravel with Laravel Entitlements image

Manage Subscription Plans and Entitlements in Laravel with Laravel Entitlements

Read article