Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update table-query-builder.md #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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