Skip to main content Skip to navigation

Guides: What are resources?

Hiker allows you to manage database records from a nice looking and highly customizable interface. To do so, you'll need to wrap the application's Models using Resource classes.

Creating Resources

Resources are situated in the app/Hiker/Resources/ directory.

One way to generate a resource is using the hiker:resource Artisan command:

php artisan hiker:resource User

This command will create the Hiker/Resources/Users/User.php file for you. Since the resource will most probably be linked to a FlowsRepository, you can directly create the Hiker/Resources/Users/UserFlows.php file by adding the --flows (or -f) option to the Artisan command:

php artisan hiker:resource User --flows

Available configuration methods

A Resource class contains several methods needed to get a Resource to work properly:

  • singularLabel
  • pluralLabel
  • icon
  • getAvailableColumns
  • getDefaultColumns
  • getDefaultOrderColumn
  • getDefaultOrderDirection

(Full list not yet documented)

static singularLabel(): string

Defines the singular label of the resource, in our case, it would be 'User'.

public static function singularLabel(): string
{
    return 'User';
}

static pluralLabel(): string

Defines the singular label of the resource, in our case, it would be 'Users'.

public static function pluralLabel(): string
{
    return 'Users';
}

static icon(): \Hiker\Components\Icon\Icon

Defines the icon of the resource, we use the Icon component to do so.

use Hiker\Components\Icon\Icon;

public static function icon(): Icon
{
    return Icon::make('user');
}

Check the Icon component's documentation for more details, such as the available default icons or how to add your own.

static getAvailableColumns(): array

Defines the list of columns that will be available for display in the tables listing the resource's records.

public static function getAvailableColumns(): array
{
    return [
        Attributes\Name::class,
        Attributes\Email::class,
        Attributes\CreatedAt::class,
        Attributes\UpdatedAt::class,
    ];
}

Each of these attribute classes will typically be defined in the resource's Attributes directory. For convenience, you can generate them using their dedicated artisan command. Let's create the Name attribute, for instance:

php artisan hiker:attribute User Name

static getDefaultColumns(): array

Defines the list of default columns that will be displayed in the tables listing the resource's records. Since the table component is configurable by the user, the other available attributes will be hidden by default but can always be activated on demand. The provided attribute identifiers should match available attribute keys.

public static function getDefaultColumns(): array
{
    return ['name', 'email', 'created_at'];
}

static getDefaultOrderColumn(): string

Defines which column will be used to order the tables by default. The provided identifier should be one of the resource's attribute keys. This method is always combined with getDefaultOrderDirection.

public static function getDefaultOrderColumn(): string
{
    return 'created_at';
}

static getDefaultOrderDirection(): string

Defines which direction (ASC or DESC) will be used to sort the tables when not overruled by the end-user's requested table configuration. When this method is not defined, resources will be sorted in ascending order (ASC) by default. This method is always combined with getDefaultOrderColumn.

public static function getDefaultOrderDirection(): string
{
    return 'DESC';
}

Attributes

As you can see, in the getAvailableColumns() method, we define the columns using an Attribute component. But there is a better alternative for doing this.

You can define attributes of a resource by using the hiker:attribute Artisan command:

php artisan hiker:attribute User Name

This command will create the Hiker/Resources/Users/Attributes/Name.php file for you.

class Name extends Attribute
{
    public $key = 'name';

    public function title()
    {
        return 'Name';
    }

    public function component($resource)
    {
        return DisplayTextfield::make($resource->name ?? null);
    }

    public function orderQuery($query, $direction) {
        return $query->orderBy('name', $direction);
    }
}

This file contains methods and attributes you'll need to make the attribute work:

  • $key: This is the attribute that will be used to reference the attribute everywhere in the code.

  • title(): Defines the display title of the attribute.

  • component($resource): Defines the component that will be used to display the attribute. By default, it's a DisplayTextfield.

  • orderQuery($query, $direction): Defines the eloquent query that will decide how the attribute column will be sorted.
    It may be ommitted provided you have defined a public $orderable attribute to false like so: public $orderable = false;.