Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions table-query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,47 @@ $table->withGlobalSearch(columns: ['name', 'email']);
$table->withGlobalSearch('Search through the data...', ['name', 'email']);
```

### Sort by Closure

The Table component supports sorting the results by Closure. For example, your model has
`full_name` [accessor](https://laravel.com/docs/10.x/eloquent-mutators#defining-an-accessor):

```php
protected $appends = [
'full_name',
];

protected $fillable = [
'first_name',
'last_name',
];

public function fullName(): Attribute
{
return Attribute::make(
get: fn () => trim(sprintf('%s %s', $this->first_name, $this->last_name)),
);
}
```

Then you can do to sort by `full_name`:

```php
$table
->withGlobalSearch(
columns: ['first_name', 'last_name'])
->column(
key: 'full_name',
sortable: fn (Builder $query, string $direction) => $query
->orderBy('first_name', $direction)
->orderBy('last_name', $direction),
)
->searchInput(
key: ['first_name', 'last_name'],
label: 'Full Name',
);
```

## Example Table

```php
Expand Down