Skip to content
This repository was archived by the owner on Oct 20, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions app/tests/Unit/SpladeQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Tests\Unit;

use App\Models\Address;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use ProtoneMedia\Splade\SpladeQueryBuilder;
use ProtoneMedia\Splade\SpladeTable;
use Tests\TestCase;

class SpladeQueryBuilderTest extends TestCase
Expand All @@ -21,4 +24,23 @@ public function it_can_parse_terms()
$this->assertEquals(['0'], $builder->parseTermsIntoCollection('0')->all());
$this->assertEquals(['1'], $builder->parseTermsIntoCollection('1')->all());
}

/** @test */
public function it_can_apply_sorting_with_a_closure()
{
$table = SpladeTable::for(Address::class)
->column('two_columns', sortable: function ($builder, $direction) {
$builder->orderBy('address', $direction)
->orderBy('city', $direction);
})
->defaultSort('two_columns');

DB::enableQueryLog();
$table->beforeRender();
DB::disableQueryLog();
$log = DB::getQueryLog();

$this->assertCount(1, $log);
$this->assertEquals('select * from "addresses" order by "address" asc, "city" asc', $log[0]['query']);
}
}
4 changes: 4 additions & 0 deletions src/SpladeQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ private function applyConstraint(array $columns, string $terms)
*/
private function applySorting(Column $column)
{
if (is_callable($column->sortable)) {
return ($column->sortable)($this->builder, $column->sorted);
}

if (!$column->isNested()) {
// Not a relationship, just a column on the table.
return $this->builder->orderBy($column->key, $column->sorted);
Expand Down
4 changes: 2 additions & 2 deletions src/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
public string $label,
public bool $canBeHidden,
public bool $hidden,
public bool $sortable,
public bool|Closure $sortable,
public bool|string $sorted,
public bool $highlight,
public bool|Closure $exportAs,
Expand Down Expand Up @@ -60,7 +60,7 @@ public function toArray()
'label' => $this->label,
'can_be_hidden' => $this->canBeHidden,
'hidden' => $this->hidden,
'sortable' => $this->sortable,
'sortable' => $this->sortable !== false,
'sorted' => $this->sorted,
'highlight' => $this->highlight,
];
Expand Down
3 changes: 2 additions & 1 deletion src/Table/HasColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ProtoneMedia\Splade\Table;

use Closure;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -48,7 +49,7 @@ public function column(
string $label = null,
bool|null $canBeHidden = null,
bool $hidden = false,
bool $sortable = false,
bool|Closure $sortable = false,
bool|string $searchable = false,
bool|null $highlight = null,
bool|callable $exportAs = true,
Expand Down