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.
Parameters:
Parameter | Type | Description |
---|---|---|
$request |
\Illuminate\Http\Request | The incoming request |
middlewares
Returns the middlewares that should be applied to the table's routes.
name
Returns the table name.
initColumns
Initializes the table columns.
shouldCache
Flags if the columns should be cached.
Usable for dynamic columns serving (from a third-party services).
shouldRefresh
Flags if the columns should be refreshed on every request.
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
getColumn
Returns specific column
Parameters:
Parameter | Type | Description |
---|---|---|
$columnName |
string |
Throws:
getCachedColumns
Returns the columns from the cache (if there are any)
getFacade
Returns a Laravel Facade of the Table class.
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.
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.
Parameters:
Parameter | Type | Description |
---|---|---|
$records |
mixed | - The target records. |
getName
Returns the table name.
additionalData
Returns the table additional data passed to the FE.
Parameters:
Parameter | Type | Description |
---|---|---|
$resource |
mixed | The data record |
getKeyName
Returns the record ID key.