Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: DataTable as route action #201

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

Merged
yajra merged 1 commit into yajra:master from ejgandelaberon:datatable-as-route-action
Oct 3, 2025
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
26 changes: 26 additions & 0 deletions src/Services/DataTable.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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.
*
Expand Down Expand Up @@ -742,4 +752,20 @@ protected function buildFastExcelFile(): FastExcel

return (new FastExcel($dataTable->toArray()['data']))->setColumnStyles($styles);
}

/**
* @return array<string, mixed>
*/
protected function viewData(): array
{
return [];
}

/**
* @return array<string, mixed>
*/
protected function viewMergeData(): array
{
return [];
}
}
18 changes: 18 additions & 0 deletions tests/DataTableServiceTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions tests/DataTables/UsersDataTable.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class UsersDataTable extends DataTable
{
protected ?string $view = 'tests::users';

public function dataTable(Builder $query): EloquentDataTable
{
return (new EloquentDataTable($query))
Expand All @@ -36,4 +38,12 @@ protected function filename(): string
{
return 'Users';
}

protected function viewData(): array
{
return [
'title' => 'LaravelDataTables',
'description' => 'This is a test description',
];
}
}
8 changes: 6 additions & 2 deletions tests/views/users.blade.php
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{{ $dataTable->table() }}
<h2>{{ $title ?? 'DataTable' }}</h2>
<p>{{ $description ?? 'No description' }}</p>
<div>
{{ $dataTable->table() }}
</div>

{{ $dataTable->scripts() }}
{{ $dataTable->scripts() }}
Loading

AltStyle によって変換されたページ (->オリジナル) /