From 6cca1af7c23f42451f2f5ec737951b1fcd2b1b1f Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Tue, 25 Sep 2018 16:27:46 +0200 Subject: [PATCH] Support MorphTo eager loading with selected columns (#25662) --- .../Database/Eloquent/Relations/MorphTo.php | 26 ++---- .../Database/EloquentMorphToSelectTest.php | 91 +++++++++++++++++++ 2 files changed, 98 insertions(+), 19 deletions(-) create mode 100644 tests/Integration/Database/EloquentMorphToSelectTest.php diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index e737a130b2a0..ac28ad3f2df7 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -235,24 +235,6 @@ public function touch() } } - /** - * Remove all or passed registered global scopes. - * - * @param array|null $scopes - * @return $this - */ - public function withoutGlobalScopes(array $scopes = null) - { - $this->getQuery()->withoutGlobalScopes($scopes); - - $this->macroBuffer[] = [ - 'method' => __FUNCTION__, - 'parameters' => [$scopes], - ]; - - return $this; - } - /** * Get the foreign key "type" name. * @@ -298,7 +280,13 @@ protected function replayMacros(Builder $query) public function __call($method, $parameters) { try { - return parent::__call($method, $parameters); + $result = parent::__call($method, $parameters); + + if (in_array($method, ['select', 'selectRaw', 'selectSub', 'addSelect', 'withoutGlobalScopes'])) { + $this->macroBuffer[] = compact('method', 'parameters'); + } + + return $result; } // If we tried to call a method that does not exist on the parent Builder instance, diff --git a/tests/Integration/Database/EloquentMorphToSelectTest.php b/tests/Integration/Database/EloquentMorphToSelectTest.php new file mode 100644 index 000000000000..c651711d3417 --- /dev/null +++ b/tests/Integration/Database/EloquentMorphToSelectTest.php @@ -0,0 +1,91 @@ +increments('id'); + $table->timestamps(); + }); + + Schema::create('comments', function (Blueprint $table) { + $table->increments('id'); + $table->string('commentable_type'); + $table->integer('commentable_id'); + }); + + $post = Post::create(); + (new Comment)->commentable()->associate($post)->save(); + } + + public function test_select() + { + $comments = Comment::with('commentable:id')->get(); + + $this->assertEquals(['id' => 1], $comments[0]->commentable->getAttributes()); + } + + public function test_select_raw() + { + $comments = Comment::with(['commentable' => function ($query) { + $query->selectRaw('id'); + }])->get(); + + $this->assertEquals(['id' => 1], $comments[0]->commentable->getAttributes()); + } + + public function test_select_sub() + { + $comments = Comment::with(['commentable' => function ($query) { + $query->selectSub(function ($query) { + $query->select('id'); + }, 'id'); + }])->get(); + + $this->assertEquals(['id' => 1], $comments[0]->commentable->getAttributes()); + } + + public function test_add_select() + { + $comments = Comment::with(['commentable' => function ($query) { + $query->addSelect('id'); + }])->get(); + + $this->assertEquals(['id' => 1], $comments[0]->commentable->getAttributes()); + } + + public function test_lazy_loading() + { + $comment = Comment::first(); + $post = $comment->commentable()->select('id')->first(); + + $this->assertEquals(['id' => 1], $post->getAttributes()); + } +} + +class Comment extends Model +{ + public $timestamps = false; + + public function commentable() + { + return $this->morphTo(); + } +} + +class Post extends Model +{ +}