Eloquent Resource
Every Inertia Table is backed by an Eloquent resource: a model class, a query builder, or a relationship. You may set the resource declaratively via the $resource property for static cases, or dynamically via the resource() method when you need to scope the query, select specific columns, or query a relationship. Stateful tables that need runtime data (like a Company instance) pass it through the constructor with the #[Remember] attribute so it survives action and export round-trips.
The easiest way to define the Eloquent model backing a Table is by setting the $resource property on your Table class:
class Users extends Table
{
protected ?string $resource = User::class;
}Alternatively, you may define the resource using the resource() method. It should return an Eloquent Builder instance or a string containing the model class name. This is useful when you need to display data based on additional criteria:
use Illuminate\Database\Eloquent\Builder;
class ActiveAdministrators extends Table
{
public function resource(): Builder|string
{
return User::where('role', 'admin')->whereNotNull('activated_at');
}
}Another use case for the resource() method is selecting specific columns from the database. For example, to display only the id and name columns of the User model:
public function resource(): Builder|string
{
return User::select(['id', 'name']);
}The resource() method may also query a relationship. For example, to display all the posts of the authenticated user:
use Illuminate\Database\Eloquent\Relations\HasMany;
class Posts extends Table
{
public function resource(): HasMany
{
return auth()->user()->posts();
}
}When using the authenticated user in the resource() method, make sure the Table component is rendered on a route protected by authentication middleware.
Stateful Resources
So far, all table resources in this documentation have been stateless, like User::class and User::where('role', 'admin'). They may contain constraints, but no runtime data is passed to them. Like any PHP class, you may add a constructor to your Table class to inject dependencies. This is useful for passing an Eloquent model instance from the controller to the Table.
use App\Models\Company;
use App\Tables\Employees;
class CompaniesController
{
public function show(Company $company)
{
return inertia('Companies/Show', [
'employees' => new Employees($company),
]);
}
}The Employees Table class now accepts a Company instance in its constructor. Use this instance to query the employees belonging to the given company:
use App\Models\Company;
use Illuminate\Database\Eloquent\Relations\HasMany;
use InertiaUI\Table\Remember;
class Employees extends Table
{
public function __construct(
#[Remember] public Company $company
) { }
public function resource(): HasMany
{
return $this->company->employees();
}
}Remember Attribute
Notice the #[Remember] attribute on the $company property in the example above. It is required whenever your Table has Actions or Exports, which run through a dedicated route. The Remember attribute preserves the Company instance in the URL of that route, so the action or export can reconstruct the original table state. As a rule of thumb, only use literal values or Eloquent models in the constructor.
The URL is encrypted, so the constructor parameters are not exposed to the user, and signed to prevent tampering. Even with these security measures, always implement proper authorization checks in your Actions and Exports.