Skip to main content

Resource metadata

Metadata for resources.

Resource name

A resource can have a custom name by overriding the following method:

public function name(): string
{
return Str::plural(Str::snake(class_name($this)));
}

Singular name

A resource can have a custom singular name by overriding the following method:

public function singularName(): string
{
return __(Str::ucfirst(Str::singular($this->name())));
}

Plural name

A resource can have a custom plural name by overriding the following method:

public function pluralName(): string
{
return __(Str::ucfirst($this->name()));
}

Icon

The icon will be used in breadcrumbs, menus, roles and permissions and more. See https://fonts.google.com/icons?selected=Material+Icons for available icons.

public function icon(): string
{
return 'person';
}

Model title

By default, a resource uses the model ID as a title (in breadcrumbs and other places). You can override this:

public function modelTitle(Model $model): string
{
return "$model->name";
}

Model description

A description is optional, and will be shown in relational fields like BelongsTo (inside dropdowns):

public function modelDescription(Model $model): ?string
{
return $model->primaryAddress->residence ?? $model->type->description;
}

Active

You can set a resource inactive. Other resources may interact with these resources (like referencing a BelongsTo field), but no actual href links will be created. An example is having a Role resource, but you do not want the user to navigate to a role detail page.

public function active(): bool
{
return false;
}

To Http Resource

When viewing a resource on the detail page, a laravel Http Resource will be used in order to hydrate the model JSON value. By default, Qore uses a DefaultResource which limits to only an id and a title. You may supply your own Http Resource:

public function toHttpResource(Model $model): mixed
{
return new UserResource($model);
}

Custom show arguments

When creating a customized detail view for your resource, you might need some data from the back-end.

You can add the following method:

public function showResponseArguments(Model $model): array
{
return [
'components' => config('invoicing.components'),
'translations' => [
'send' => __('invoicing::invoicing.Send'),
'download' => __('invoicing::invoicing.Download'),
]
];
}

Enabling / Disabling pages

You can disable specific resource pages:

    public function hasIndexPage(): bool
{
return true;
}

public function hasEditPage(): bool
{
return true;
}

public function hasShowPage(): bool
{
return true;
}

public function hasCreatePage(): bool
{
return true;
}

You need to clear your Vuex cache in order to see the changes.

Custom redirect URL

After creating or editing a resource, you can set a custom redirect URL:

public function redirectUrl(?Model $model = null): ?string
{
return '/home';
}

Duplicating

You can enable duplicating a resource by adding:

public function duplicateEnabled(): bool
{
return true;
}

This will automatically add a duplicate button on the resource detail page. When this button is clicked, the user will be redirected to the create page where every field value is copied.

The form state is aware of duplication:

Text::make(...)
->default(function (ManagesForm $form) {
if ($model = $form->getDuplicateModel()) {
return 'duplicating'
}

return 'not duplicating';
})

You can also override the state of the duplication form:

class TicketResource extends QoreResource
{
public function duplicateEnabled(): bool
{
return true;
}

public function duplicateInitialState(Model $original, array $computedState): array
{
return array_merge($computedState, [
'name' => $original->name . ' (copy)'
]);
}
}

Duplicating also emits events that you can react to, please see Resource Events - Duplication

Create another one

Often users want to create multiple records in a quick succession. This means that the user should not be redirected to the resource detail page after creating a resource.

You can enable this behaviour on your resource:

function isCreateAnotherOneAllowed(): bool
{
return true;
}