Skip to content

Transform Eloquent Model Data

By default, Inertia Table only sends the columns it needs to render to the frontend, derived from your Table class's columns() definition. For full control over the row payload (to mask emails, add computed properties, rename keys, or merge in extra data), implement transformModel() on your Table class.

Introduction

The built-in Query Builder only ships the data your columns actually need. For example, a users table with id, name, email, and password rendered through an Inertia Table that only uses name and email will only send those two attributes to the client.

The mapAs argument transforms data per column, but sometimes you need full control over the entire row payload.

Transforming Data

You may transform an Eloquent model's data via the transformModel method on your Table class. It receives the Eloquent model instance and the auto-derived data array, and should return an array of the transformed data.

php
use Illuminate\Support\Str;

class Users extends Table
{
    public function transformModel(Model $model, array $data): array
    {
        return [
            'key' => $model->id,
            'name' => $model->generateFullName(),
            'email' => Str::mask($model->email),
        ];
    }
}

In the example above, $data is unused and a whole new array is returned. To add or change just a few fields instead, merge with $data:

php
class Users extends Table
{
    public function transformModel(Model $model, array $data): array
    {
        return [
            ...$data,
            'avatar_url' => $user->avatar_path ? Storage::url($user->avatar_path) : null,
        ];
    }
}