Skip to content

Commit 24e71b5

Browse files
authored
Merge pull request #2789 from sebdesign/eloquent-relation
Create eloquent datatable from relation
2 parents 85a1186 + 97c3bd9 commit 24e71b5

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/EloquentDataTable.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
1010
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
1111
use Illuminate\Database\Eloquent\Relations\MorphTo;
12+
use Illuminate\Database\Eloquent\Relations\Relation;
1213
use Yajra\DataTables\Exceptions\Exception;
1314

1415
/**
@@ -19,22 +20,15 @@ class EloquentDataTable extends QueryDataTable
1920
/**
2021
* EloquentEngine constructor.
2122
*
22-
* @param \Illuminate\Database\Eloquent\Model|EloquentBuilder $model
23-
*
24-
* @throws \Yajra\DataTables\Exceptions\Exception
23+
* @param Model|EloquentBuilder $model
2524
*/
26-
public function __construct($model)
25+
public function __construct(Model|EloquentBuilder $model)
2726
{
28-
switch ($model) {
29-
case $model instanceof Model:
30-
$builder = $model->newQuery();
31-
break;
32-
case $model instanceof EloquentBuilder:
33-
$builder = $model;
34-
break;
35-
default:
36-
throw new Exception('Invalid model type. Must be an instance of Eloquent Model or Eloquent Builder.');
37-
}
27+
$builder = match (true) {
28+
$model instanceof Model => $model->newQuery(),
29+
$model instanceof Relation => $model->getQuery(),
30+
$model instanceof EloquentBuilder => $model,
31+
};
3832

3933
parent::__construct($builder->getQuery());
4034

@@ -100,10 +94,13 @@ protected function compileQuerySearch($query, string $column, string $keyword, s
10094
}
10195

10296
if ($this->isMorphRelation($relation)) {
103-
$query->{$boolean.'WhereHasMorph'}($relation, '*',
97+
$query->{$boolean.'WhereHasMorph'}(
98+
$relation,
99+
'*',
104100
function (EloquentBuilder $query) use ($newColumn, $keyword) {
105101
parent::compileQuerySearch($query, $newColumn, $keyword, '');
106-
});
102+
}
103+
);
107104
} else {
108105
$query->{$boolean.'WhereHas'}($relation, function (EloquentBuilder $query) use ($newColumn, $keyword) {
109106
parent::compileQuerySearch($query, $newColumn, $keyword, '');

tests/Integration/EloquentDataTableTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ public function it_can_return_formatted_columns()
121121
$this->assertEquals(Carbon::parse($user->created_at)->format('Y-m-d'), $data['created_at_formatted']);
122122
}
123123

124+
/** @test */
125+
public function it_accepts_a_relation()
126+
{
127+
$user = User::first();
128+
129+
$dataTable = app('datatables')->eloquent($user->posts());
130+
$response = $dataTable->toJson();
131+
$this->assertInstanceOf(EloquentDataTable::class, $dataTable);
132+
$this->assertInstanceOf(JsonResponse::class, $response);
133+
}
134+
124135
protected function setUp(): void
125136
{
126137
parent::setUp();

0 commit comments

Comments
 (0)