-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support MorphTo eager loading with selected columns (#25662)
- Loading branch information
1 parent
2d78e5d
commit 6cca1af
Showing
2 changed files
with
98 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |