Skip to content

Commit

Permalink
Support MorphTo eager loading with selected columns (#25662)
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir authored and taylorotwell committed Sep 25, 2018
1 parent 2d78e5d commit 6cca1af
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 19 deletions.
26 changes: 7 additions & 19 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
Expand Down
91 changes: 91 additions & 0 deletions tests/Integration/Database/EloquentMorphToSelectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentMorphToSelectTest;

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;

/**
* @group integration
*/
class EloquentMorphToSelectTest extends DatabaseTestCase
{
public function setUp()
{
parent::setUp();

Schema::create('posts', function (Blueprint $table) {
$table->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
{
}

0 comments on commit 6cca1af

Please sign in to comment.