Skip to content

Commit

Permalink
Merge pull request #2818 from yajra/patch1
Browse files Browse the repository at this point in the history
[10.x] Fix HasOneThrough
  • Loading branch information
yajra authored Jul 12, 2022
2 parents 22e6dcf + 3a861cd commit 36287cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/EloquentDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,18 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)

case $model instanceof HasOneThrough:
$pivot = explode('.', $model->getQualifiedParentKeyName())[0]; // extract pivot table from key
$pivotPK = $pivot.'.'.$model->getLocalKeyName();
$pivotPK = $pivot.'.'.$model->getFirstKeyName();
$pivotFK = $model->getQualifiedLocalKeyName();
$this->performJoin($pivot, $pivotPK, $pivotFK);

$related = $model->getRelated();
$table = $related->getTable();
$tablePK = $related->getForeignKey();
$tablePK = $model->getSecondLocalKeyName();
$foreign = $pivot.'.'.$tablePK;
$other = $related->getQualifiedKeyName();

$lastQuery->addSelect($lastQuery->getModel()->getTable().'.*');

break;

case $model instanceof HasOneOrMany:
Expand Down
29 changes: 29 additions & 0 deletions tests/Integration/HasOneThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ public function it_returns_all_records_with_the_relation_when_called_without_par
$this->assertCount(60, $response->json()['data']);
}

/** @test */
public function it_can_search_has_one_through_relation()
{
$response = $this->call('GET', '/relations/hasOneThroughSearchRelation', [
'columns' => [
[
'data' => 'heart.size',
'searchable' => true,
'search' => [
'value' => 'heart-1',
],
],
],
]);

$response->assertJson([
'draw' => 0,
'recordsTotal' => 60,
'recordsFiltered' => 33,
]);

$this->assertArrayHasKey('heart', $response->json()['data'][0]);
$this->assertCount(33, $response->json()['data']);
}

/** @test */
public function it_returns_all_records_with_the_deleted_relation_when_called_with_withtrashed_parameter()
{
Expand Down Expand Up @@ -101,6 +126,10 @@ protected function setUp(): void
return $datatables->eloquent(Post::with('heart')->select('posts.*'))->toJson();
});

$this->app['router']->get('/relations/hasOneThroughSearchRelation', function (DataTables $datatables) {
return $datatables->eloquent(Post::with('heart'))->addColumns(['hearts.size'])->toJson();
});

$this->app['router']->get('/relations/hasOneThroughWithTrashed', function (DataTables $datatables) {
return $datatables->eloquent(Post::with(['heart' => function ($query) {
$query->withTrashed();
Expand Down

0 comments on commit 36287cd

Please sign in to comment.