Resources

Adonis Cockpit is currently in prerelease version. Documentation, references and examples might not be correct and are subject to change. Give your feedback.

Resources

Resources are the core of Cockpit. It represents an administrable resource.

You can manage any kind of resources and by default Cockpit ship with a ModelResource to manage Lucid Models. If you want to create custom resources, head over to the dedicated Custom Resources page.

Defining Resources

Cockpit bring a make:resource command to easily create and register new resources.

node ace make:resource User
  1. Creates a new app/cockpit/user_resource.ts

    import User from "#models/user";
    export default class UserResource {
    model = User;
    }
  2. Registers the created resource in start/cockpit.ts

    import cockpit from "adonis-cockpit/services/main";
    import UserResource from "#cockpit/user_resource";
    cockpit.resource(UserResource);

By default, every Cockpit elements are generated inside app/cockpit folder. You can change this behavior with the cockpitPath configuration option. Learn more on the Configuration page.

Registering Resources

To make any Resource available in your Cockpit dashboard, you need to register it in the start/cockpit.ts file using the cockpit.resource method.

Adonis Cockpit make:command automatically registers resources for you.

import cockpit from "adonis-cockpit/services/main";
import UserResource from "#cockpit/user_resource";
cockpit.resource(UserResource);

Resource configuration

Fields

The fields defines what data will be shown and how it will be displayed. You can configure the fields inside the fields method of your resource.

import { Id, Text, Email, Boolean } from "adonis-cockpit/fields";
export default class UserResource extends ModelResource {
fields() {
return [
Id.make("id"),
Text.make("firstName"),
Text.make("lastName"),
Email.make("email"),
Boolean.make("isAdmin"),
];
}
}

Read more about the different available fields and their configuration on the dedicated Fields page.

Labels and Slug

By default, the ModelResource automatically setup everything for you. But you might want to configure the labels and the slug that will be used across the administration pages of your resource.

export default class MemberResource extends ModelResource {
label() {
return "User";
}
labelPlural() {
return "Users";
}
slug() {
return "users";
}
}

Actions

Actions are operations that can be applied with one or multiple records. You can override the default actions and add custom ones by overriding the actions method.

export default class MemberResource extends ModelResource {
actions(): Action[] {
return [
SynchronizeAction.make(),
DeleteAction.make(),
]
}
}

Read more about how to create custom Actions on the dedicated Actions documentation page