diff --git a/src/EloquentDataTable.php b/src/EloquentDataTable.php index 072108c3..79336919 100644 --- a/src/EloquentDataTable.php +++ b/src/EloquentDataTable.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\HasOneOrMany; use Illuminate\Database\Eloquent\Relations\HasOneThrough; use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Database\Eloquent\Relations\Relation; use Yajra\DataTables\Exceptions\Exception; /** @@ -19,22 +20,15 @@ class EloquentDataTable extends QueryDataTable /** * EloquentEngine constructor. * - * @param \Illuminate\Database\Eloquent\Model|EloquentBuilder $model - * - * @throws \Yajra\DataTables\Exceptions\Exception + * @param Model|EloquentBuilder $model */ - public function __construct($model) + public function __construct(Model|EloquentBuilder $model) { - switch ($model) { - case $model instanceof Model: - $builder = $model->newQuery(); - break; - case $model instanceof EloquentBuilder: - $builder = $model; - break; - default: - throw new Exception('Invalid model type. Must be an instance of Eloquent Model or Eloquent Builder.'); - } + $builder = match (true) { + $model instanceof Model => $model->newQuery(), + $model instanceof Relation => $model->getQuery(), + $model instanceof EloquentBuilder => $model, + }; parent::__construct($builder->getQuery()); @@ -100,10 +94,13 @@ protected function compileQuerySearch($query, string $column, string $keyword, s } if ($this->isMorphRelation($relation)) { - $query->{$boolean.'WhereHasMorph'}($relation, '*', + $query->{$boolean.'WhereHasMorph'}( + $relation, + '*', function (EloquentBuilder $query) use ($newColumn, $keyword) { parent::compileQuerySearch($query, $newColumn, $keyword, ''); - }); + } + ); } else { $query->{$boolean.'WhereHas'}($relation, function (EloquentBuilder $query) use ($newColumn, $keyword) { parent::compileQuerySearch($query, $newColumn, $keyword, ''); diff --git a/tests/Integration/EloquentDataTableTest.php b/tests/Integration/EloquentDataTableTest.php index 0402bc8f..1b6760ea 100644 --- a/tests/Integration/EloquentDataTableTest.php +++ b/tests/Integration/EloquentDataTableTest.php @@ -121,6 +121,17 @@ public function it_can_return_formatted_columns() $this->assertEquals(Carbon::parse($user->created_at)->format('Y-m-d'), $data['created_at_formatted']); } + /** @test */ + public function it_accepts_a_relation() + { + $user = User::first(); + + $dataTable = app('datatables')->eloquent($user->posts()); + $response = $dataTable->toJson(); + $this->assertInstanceOf(EloquentDataTable::class, $dataTable); + $this->assertInstanceOf(JsonResponse::class, $response); + } + protected function setUp(): void { parent::setUp();