Skip to content

Commit 36287cd

Browse files
authored
Merge pull request #2818 from yajra/patch1
[10.x] Fix HasOneThrough
2 parents 22e6dcf + 3a861cd commit 36287cd

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/EloquentDataTable.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,18 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
196196

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

203203
$related = $model->getRelated();
204204
$table = $related->getTable();
205-
$tablePK = $related->getForeignKey();
205+
$tablePK = $model->getSecondLocalKeyName();
206206
$foreign = $pivot.'.'.$tablePK;
207207
$other = $related->getQualifiedKeyName();
208208

209+
$lastQuery->addSelect($lastQuery->getModel()->getTable().'.*');
210+
209211
break;
210212

211213
case $model instanceof HasOneOrMany:

tests/Integration/HasOneThroughTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ public function it_returns_all_records_with_the_relation_when_called_without_par
2626
$this->assertCount(60, $response->json()['data']);
2727
}
2828

29+
/** @test */
30+
public function it_can_search_has_one_through_relation()
31+
{
32+
$response = $this->call('GET', '/relations/hasOneThroughSearchRelation', [
33+
'columns' => [
34+
[
35+
'data' => 'heart.size',
36+
'searchable' => true,
37+
'search' => [
38+
'value' => 'heart-1',
39+
],
40+
],
41+
],
42+
]);
43+
44+
$response->assertJson([
45+
'draw' => 0,
46+
'recordsTotal' => 60,
47+
'recordsFiltered' => 33,
48+
]);
49+
50+
$this->assertArrayHasKey('heart', $response->json()['data'][0]);
51+
$this->assertCount(33, $response->json()['data']);
52+
}
53+
2954
/** @test */
3055
public function it_returns_all_records_with_the_deleted_relation_when_called_with_withtrashed_parameter()
3156
{
@@ -101,6 +126,10 @@ protected function setUp(): void
101126
return $datatables->eloquent(Post::with('heart')->select('posts.*'))->toJson();
102127
});
103128

129+
$this->app['router']->get('/relations/hasOneThroughSearchRelation', function (DataTables $datatables) {
130+
return $datatables->eloquent(Post::with('heart'))->addColumns(['hearts.size'])->toJson();
131+
});
132+
104133
$this->app['router']->get('/relations/hasOneThroughWithTrashed', function (DataTables $datatables) {
105134
return $datatables->eloquent(Post::with(['heart' => function ($query) {
106135
$query->withTrashed();

0 commit comments

Comments
 (0)