Skip to content

Views (Bookmarks)

Views (also known as bookmarks) let users save a combination of filters, sort, and column visibility under a name, then switch between them with one click. They're persisted to the database via a published migration, scoped per user by default through Laravel's authentication, and may be restricted to specific groups or made global with a custom resolver.

Getting Started

To use the Views feature, publish the database migration for the views table:

bash
php artisan vendor:publish --tag=inertia-table-migrations

A migration file will appear in the database/migrations directory. Run the migration to create the views table:

bash
php artisan migrate

Defining Views

To enable Views, implement the views() method on your Table class. It should return an instance of the Views class.

php
use InertiaUI\Table\Views;

class Users extends Table
{
    public function views(): ?Views
    {
        return Views::make();
    }
}

Customizing Views

By default, Views are stored per user. The package uses Laravel's built-in authentication system and the Auth facade to determine the current user. You may disable this behavior or pass a custom resolver to retrieve the user ID.

Disabling User-based Views

To disable user-based views, pass false to the scopeUser argument of Views::make(), or chain the scopeUser method on the Views instance. This shares views across all users, so use it with care.

php
Views::make(scopeUser: false);

Views::make()->scopeUser(false);

To disable this feature globally, call the static defaultScopeUser method, typically in your AppServiceProvider:

php
use InertiaUI\Table\Views;

Views::defaultScopeUser(false);

Custom User ID Resolver

To use a custom resolver for the user ID, pass a closure to the userResolver argument or chain the userResolver method on the Views instance:

php
Views::make(userResolver: fn () => current_user()->uuid);

Views::make()->userResolver(fn () => current_user()->uuid);

To set a global resolver, call the static defaultUserResolver method in your AppServiceProvider:

php
use InertiaUI\Table\Views;

Views::defaultUserResolver(
    fn () => current_user()->uuid
);

Custom Attributes

You may add extra columns to the views migration and define attributes used to both query and store the views. Pass an array (or a closure returning an array) to the attributes argument of Views::make(), or chain the attributes method on the Views instance:

php
Views::make(attributes: ['tenant_id' => tenant_id()]);

Views::make()->attributes(fn () => ['tenant_id' => tenant_id()]);

To set a global attributes array, call the static defaultAttributes method, typically in your AppServiceProvider:

php
use InertiaUI\Table\Views;

Views::defaultAttributes(['tenant_id' => tenant_id()]);

Table Name Scoping

By default, the Views for a Table class are not scoped by the Table name. Reusing the same Table class across different pages means they share the same stored views. This is what you want most of the time, since Table names are typically only used for multiple tables on the same page.

To scope Views by the Table name anyway, pass true to the scopeTableName argument of Views::make(), or chain the scopeTableName method on the Views instance:

php
Views::make(scopeTableName: true);

Views::make()->scopeTableName(true);

To set this globally, call the static defaultScopeTableName method, typically in your AppServiceProvider:

php
use InertiaUI\Table\Views;

Views::defaultScopeTableName(true);

Stateful Resource Scoping

By default, the Views for a Table class are not scoped by the stateful resources. Imagine an Employees Table that accepts a Company Eloquent model instance in its constructor:

php
class Employees extends Table
{
    public function __construct(
        #[Remember] public Company $company
    ) { }
}

By default, the Views are stored independently of the Company instance, so it doesn't matter which Company is passed to the Employees Table class.

To scope Views by the stateful resources, pass true to the scopeStatefulResources argument of Views::make(), or chain the scopeStatefulResources method on the Views instance:

php
Views::make(scopeStatefulResources: true);

Views::make()->scopeStatefulResources(true);

To set this globally, call the static defaultScopeStatefulResources method, typically in your AppServiceProvider:

php
use InertiaUI\Table\Views;

Views::defaultScopeStatefulResources(true);

Custom Eloquent Model

The Views feature uses the built-in InertiaUI\Table\TableView Eloquent model. To use a custom model, pass the class name to the modelClass argument of Views::make(), or chain the modelClass method on the Views instance:

php
Views::make(modelClass: CustomTableView::class);

Views::make()->modelClass(CustomTableView::class);

To set a global model class, call the static defaultModelClass method, typically in your AppServiceProvider:

php
use InertiaUI\Table\Views;

Views::defaultModelClass(CustomTableView::class);

Reading the source of the InertiaUI\Table\TableView model is recommended to see which scopes are defined and how they are used.

Custom Texts

Texts used in the Views feature, such as the "Save View" button, live in the translation file. See the Translations documentation for details on customizing them.

Default Views

Instead of defining the Views in each Table class, you may define them globally via the static defaultViewsResolver method on the Table class, typically in your AppServiceProvider:

php
use InertiaUI\Table\Table;
use InertiaUI\Table\Views;

Table::defaultViewsResolver(
    fn (Table $table) => Views::make()->scopeUser(false)
);

The resolver applies to all Table classes unless overridden on a specific one. The callback receives the Table instance, so you may customize the Views per table.