Skip to content

Table Definition Classes

Description

All table structures should be defined in the /app/Tables directory and should inherit the MtrDesign\Krait\Tables\BaseTable class.

By running php artisan krait:table MyAwesomeTable, Krait will generate the directory and the MyAwesomeTable class automatically for you.

Example

<?php

namespace App\Tables;

use MtrDesign\Krait\Tables\BaseTable;

class MyAwesomeTable extends BaseTable
{
    function name(): string
    {
        return 'my-awesome-table';
    }

    function initColumns(): void
    {
        $this->column(
            name: 'my_first_column',
            label: 'My First Column',
            process: fn(mixed $resource) => 'This is the processed content!'
        );

        $this->column(
            name: 'some_field',
            label: 'Resource Field',
        );
    }

    function additionalData(mixed $resource): array
    {
        return [
            'additional_prop' => 'Krait is awesome!',
        ];
    }
}

Methods


authorize

Flags if a request is authorized to retrieve the table's data.

public authorize(\Illuminate\Http\Request $request): boolean

Parameters:

Parameter Type Description
$request \Illuminate\Http\Request The incoming request

middlewares

Returns the middlewares that should be applied to the table's routes.

public middlewares(): array

name

Returns the table name.

public name(): string

initColumns

Initializes the table columns.

public initColumns(): void

shouldCache

Flags if the columns should be cached.

protected shouldCache(): bool

Usable for dynamic columns serving (from a third-party services).


shouldRefresh

Flags if the columns should be refreshed on every request.

protected shouldRefresh(): bool

Usable for dynamic columns serving (from a third-party services).


column

Adds a column to the table

protected column(
    string $name,
    string $label,
    bool $hideLabel = false,
    bool $datetime = false,
    bool $sortable = true,
    bool $fixed = false,
    string|null $classes = null,
    callable|null $process = null,
    callable|null $sort = null
): void

Parameters:

Parameter Type Description
$name string - The columns name
$label string - The columns label
$hideLabel bool - Flags if the label should be visible in the header.
$datetime bool - Flags if the column contains datetime object.
$sortable bool - Flags if the column is sortable.
$fixed bool - Flags if the column is resizable.
$classes string|null - Additional classes that will be added on FE.
$process callable|null - The column result generation callback.
$sort callable|null - The column sorting callback.

getColumns

Returns all columns

public getColumns(): \MtrDesign\Krait\DTO\TableColumnDTO[]

getColumn

Returns specific column

public getColumn(string $columnName): \MtrDesign\Krait\DTO\TableColumnDTO

Parameters:

Parameter Type Description
$columnName string

Throws:


getCachedColumns

Returns the columns from the cache (if there are any)

protected getCachedColumns(): ?array

getFacade

Returns a Laravel Facade of the Table class.

protected static getFacade(): \MtrDesign\Krait\Tables\BaseTable

processRecord

Processes one record.

public processRecord(\Illuminate\Database\Eloquent\Model|array $resource, mixed|null $placeholder = null): array|mixed

Parameters:

Parameter Type Description
$resource \Illuminate\Database\Eloquent\Model|array - The record.
$placeholder mixed|null - The placeholder for empty values.

process

Processes a record.

public static process(mixed $resource, mixed|null $placeholder = null): mixed

Parameters:

Parameter Type Description
$resource mixed - The target record.
$placeholder mixed|null - The placeholder for empty values.

from

Generates an API Resource Collection for the table.

public static from(mixed $records): \MtrDesign\Krait\Http\Resources\TableCollection

Parameters:

Parameter Type Description
$records mixed - The target records.

getName

Returns the table name.

public static getName(): string

additionalData

Returns the table additional data passed to the FE.

public additionalData(mixed $resource): array

Parameters:

Parameter Type Description
$resource mixed The data record

getKeyName

Returns the record ID key.

public getKeyName(): string