From 6e2e0c3be6c9b78455502191ad1e6b491491f20e Mon Sep 17 00:00:00 2001 From: Emmanuel Joseph Beron Date: Fri, 3 Oct 2025 09:55:17 +0800 Subject: [PATCH] feat: datatable as route action --- src/Services/DataTable.php | 26 ++++++++++++++++++++++++++ tests/DataTableServiceTest.php | 18 ++++++++++++++++++ tests/DataTables/UsersDataTable.php | 10 ++++++++++ tests/views/users.blade.php | 8 ++++++-- 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/Services/DataTable.php b/src/Services/DataTable.php index 64b1a3e..30c11bb 100644 --- a/src/Services/DataTable.php +++ b/src/Services/DataTable.php @@ -151,6 +151,11 @@ abstract class DataTable implements DataTableButtons */ protected string $pdfWriter = 'Dompdf'; + /** + * @phpstan-var view-string|null + */ + protected ?string $view = null; + public function __construct() { /** @var Request $request */ @@ -163,6 +168,11 @@ public function __construct() $this->htmlBuilder = $builder; } + public function __invoke(): mixed + { + return $this->render($this->view, $this->viewData(), $this->viewMergeData()); + } + /** * Process dataTables needed render output. * @@ -742,4 +752,20 @@ protected function buildFastExcelFile(): FastExcel return (new FastExcel($dataTable->toArray()['data']))->setColumnStyles($styles); } + + /** + * @return array + */ + protected function viewData(): array + { + return []; + } + + /** + * @return array + */ + protected function viewMergeData(): array + { + return []; + } } diff --git a/tests/DataTableServiceTest.php b/tests/DataTableServiceTest.php index 91b1b66..74bab7c 100644 --- a/tests/DataTableServiceTest.php +++ b/tests/DataTableServiceTest.php @@ -4,6 +4,7 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Http\Response; +use Illuminate\Routing\Router; use Illuminate\Support\Collection; use PHPUnit\Framework\Attributes\Test; use Symfony\Component\HttpFoundation\BinaryFileResponse; @@ -95,6 +96,23 @@ public function it_is_macroable(): void $this->assertEquals('macro', $dataTable->macroMethod()); } + #[Test] + public function it_can_be_used_as_route_action(): void + { + /** @var Router|null $router */ + $router = $this->app['router'] ?? null; + $router?->get('datatables-as-route-action', UsersDataTable::class); + + $this->get('datatables-as-route-action') + ->assertSeeText('LaravelDataTables') + ->assertSeeText('This is a test description'); + + // Assert that view data are not present when manually calling render + $this->get('users') + ->assertSeeText('DataTable') + ->assertSeeText('No description'); + } + protected function setUp(): void { parent::setUp(); diff --git a/tests/DataTables/UsersDataTable.php b/tests/DataTables/UsersDataTable.php index 0a05c98..a717651 100644 --- a/tests/DataTables/UsersDataTable.php +++ b/tests/DataTables/UsersDataTable.php @@ -10,6 +10,8 @@ class UsersDataTable extends DataTable { + protected ?string $view = 'tests::users'; + public function dataTable(Builder $query): EloquentDataTable { return (new EloquentDataTable($query)) @@ -36,4 +38,12 @@ protected function filename(): string { return 'Users'; } + + protected function viewData(): array + { + return [ + 'title' => 'LaravelDataTables', + 'description' => 'This is a test description', + ]; + } } diff --git a/tests/views/users.blade.php b/tests/views/users.blade.php index 940b47c..8ea8542 100644 --- a/tests/views/users.blade.php +++ b/tests/views/users.blade.php @@ -1,3 +1,7 @@ -{{ $dataTable->table() }} +

{{ $title ?? 'DataTable' }}

+

{{ $description ?? 'No description' }}

+
+ {{ $dataTable->table() }} +
-{{ $dataTable->scripts() }} \ No newline at end of file +{{ $dataTable->scripts() }}