Beartropy Tables
Default Sort Order
Set the initial column and direction when the table first loads.
Default Sorting in settings()
setSortColumn(string $column) — Set the default column to sort by.
setSortDirectionAsc(bool $bool) — Set direction to ascending.
setSortDirectionDesc(bool $bool) — Set direction to descending.
1public function settings(): void2{3 $this->setSortColumn('name');4 $this->setSortDirectionDesc(true);5}
Column-Level Sorting
Enable sorting on individual columns with optional custom logic.
Enable & Custom Sorting
sortable(bool|Closure $callback = true) — Pass true for standard sorting, or a closure for custom query logic. The closure receives $query and $direction.
1Column::make('Name', 'name')2 ->sortable() // Enable sorting on click3 4Column::make('Domain')5 ->sortable(function ($query, $direction) {6 $query->orderByRaw("SUBSTR(email, INSTR(email, '@') + 1) {$direction}");7 })
Sort by Different Column
sortColumnBy(string $column_key) — Override the database column used for sorting. Useful when the display is a computed value but you want to sort by a real database field.
1Column::make('Full Name')2 ->sortColumnBy('lastname')
Custom closures unlock powerful sorting
SUBSTR or LOWER.
Complete Example
A playground table with standard and custom sorting on a computed 'Domain' column.
SortableUsersTable.php
This example sorts by name by default (ascending), includes standard sortable columns, and has a computed "Domain" column that sorts by the email domain using a raw SQL expression.
1use App\Models\User; 2use Beartropy\Tables\Classes\Columns\Column; 3use Beartropy\Tables\Classes\Columns\DateColumn; 4use Beartropy\Tables\YATBaseTable; 5 6class SortableUsersTable extends YATBaseTable 7{ 8 public $model = User::class; 9 10 public function columns(): array11 {12 return [13 Column::make('Name', 'name')14 ->sortable()->searchable(),15 16 Column::make('Email', 'email')17 ->sortable()->searchable(),18 19 Column::make('Domain')20 ->sortable(function ($query, $direction) {21 $query->orderByRaw("SUBSTR(email, INSTR(email, '@') + 1) {$direction}");22 })23 ->searchable(function ($query, $search) {24 $query->orWhereRaw("SUBSTR(email, INSTR(email, '@') + 1) LIKE ?", ["%{$search}%"]);25 }),26 27 DateColumn::make('Created', 'created_at')28 ->outputFormat('M d, Y')->sortable(),29 ];30 }31 32 public function settings(): void33 {34 $this->setPerPageDefault(15);35 $this->setSortColumn('name');36 $this->setSortDirectionAsc(true);37 }38}
Live Demo
Try it — click column headers to sort ascending and descending.
Sortable Table
| # |
Name
↑
|
Email
|
Score
|
Joined
|
|---|---|---|---|---|
| 1 | Alice Johnson | alice@example.com | 95 | Jan 15, 2024 |
| 2 | Bob Smith | bob@example.com | 82 | Feb 20, 2024 |
| 3 | Carol White | carol@example.com | 91 | Mar 10, 2024 |
| 4 | David Brown | david@example.com | 78 | Apr 05, 2024 |
| 5 | Eva Martinez | eva@example.com | 88 | May 22, 2024 |
| 6 | Frank Lee | frank@example.com | 73 | Jun 18, 2024 |
| 7 | Grace Kim | grace@example.com | 97 | Jul 03, 2024 |
| 8 | Henry Davis | henry@example.com | 85 | Aug 29, 2024 |
1<livewire:tables.demos.demo-sortable-table />