2 Columns
Lenny P. edited this page 2026-10-19 13:53:40 +02:00

Columns

Import the column class:

use RealZone22\PenguTables\Table\Column;

Create a column

Column::make('Name', 'name')

The first argument is the displayed label. The second argument is the model attribute used to read the value.

Sorting

Column::make('Name', 'name')->sortable()

When a column is sortable, clicking its heading changes the table sort. The sort field is passed to the query as an orderBy field, so only use trusted model attributes as column keys.

Column::make('Email', 'email')->searchable()

Searchable columns are used by the global table search. Multiple search terms are split by spaces and matched against all searchable columns.

Custom search queries

Use searchUsing() when a column needs a custom database query:

use Illuminate\Database\Eloquent\Builder;

Column::make('Causer', 'causer_id')
    ->searchable()
    ->searchUsing(function (Builder $query, string $term): void {
        $query->orWhereHas('causer', function (Builder $causerQuery) use ($term): void {
            $causerQuery->where('display_name', 'LIKE', '%'.$term.'%');
        });
    });

The callback receives the current table query and one search term. Add conditions with orWhere, orWhereHas, or other Eloquent query methods. searchable() must still be enabled on the column.

Hide a column

Column::make('Internal ID', 'id')->hidden()

Hidden columns are removed from the rendered table. Use hideIf() when the visibility depends on runtime logic:

Column::make('Admin Notes', 'admin_notes')
    ->hideIf(fn () => ! auth()->user()->isAdmin());

Format a value

Column::make('Created', 'created_at')
    ->format(fn ($value) => $value?->format('Y-m-d H:i:s'));

The callback receives the attribute value and, optionally, the complete row:

Column::make('Name', 'name')
    ->format(fn ($value, $user) => $user->first_name.' '.$user->last_name);

Formatted values are escaped by default.

Render HTML

Use html() only when the formatted value is trusted HTML:

Column::make('Status', 'status')
    ->format(fn ($value) => '<span class="badge">'.$value.'</span>')
    ->html();

Do not pass unescaped user input into an HTML column.

Exports

Columns are included in exports by default according to their hideInExport setting. To exclude a column:

Column::make('Actions', null)
    ->hideInExport();

Action column

Use Column::actions() to render row-specific actions:

Column::actions('Actions', function ($user) {
    return [
        Action::make(
            '<a href="/users/'.$user->id.'/edit">Edit</a>'
        ),
    ];
})

Action columns are automatically marked as HTML and hidden from exports.