From 2ce88d71f4264818e043a1e6614cc0d6b2f37234 Mon Sep 17 00:00:00 2001
From: Dzianis Kotau <me@dzianiskotau.com>
Date: Tue, 4 Apr 2023 00:21:29 +0400
Subject: [PATCH] Update table-query-builder.md

---
 table-query-builder.md | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/table-query-builder.md b/table-query-builder.md
index 5bdc65f..482ea81 100644
--- a/table-query-builder.md
+++ b/table-query-builder.md
@@ -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