From 1618a638f6c571bac4599ff62b5e6875db91ec69 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Tue, 18 Dec 2018 19:14:25 +0100 Subject: [PATCH] Remove temporary columns from results --- src/Builder.php | 29 +++++++++++++++++++++++++++++ tests/HasEagerLimitTest.php | 3 +++ 2 files changed, 32 insertions(+) diff --git a/src/Builder.php b/src/Builder.php index 2aacc05..cf5367f 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -28,4 +28,33 @@ public function groupLimit($value, $column) return $this; } + + /** + * Execute the query as a "select" statement. + * + * @param array $columns + * @return \Illuminate\Support\Collection + */ + public function get($columns = ['*']) + { + $results = parent::get($columns); + + if (! $this->groupLimit) { + return $results; + } + + $column = last(explode('.', $this->groupLimit['column'])); + + $keys = [ + 'laravel_row', + '@laravel_partition := '.$this->grammar->wrap($column), + '@laravel_partition := '.$this->grammar->wrap('pivot_'.$column), + ]; + + foreach ($results as $result) { + unset($result->{$keys[0]}, $result->{$keys[1]}, $result->{$keys[2]}); + } + + return $results; + } } diff --git a/tests/HasEagerLimitTest.php b/tests/HasEagerLimitTest.php index 8359db9..5cbfc5f 100644 --- a/tests/HasEagerLimitTest.php +++ b/tests/HasEagerLimitTest.php @@ -14,6 +14,8 @@ public function testHasOne() $this->assertEquals(3, $users[0]->post->id); $this->assertEquals(6, $users[1]->post->id); + $this->assertArrayNotHasKey('laravel_row', $users[0]->post); + $this->assertArrayNotHasKey('@laravel_partition := `user_id`', $users[0]->post); } public function testMorphOne() @@ -54,6 +56,7 @@ public function testBelongsToMany() $this->assertEquals([3, 2], $users[0]->roles->pluck('id')->all()); $this->assertEquals([6, 5], $users[1]->roles->pluck('id')->all()); + $this->assertArrayNotHasKey('@laravel_partition := `pivot_user_id`', $users[0]->roles[0]); } public function testMorphToMany()