Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Allow eager-loading mixed relationships of polymorphic collection #23626

Merged
merged 1 commit into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ public function load($relations)
return $this;
}

/**
* Load a set of relationships onto the mixed relationship collection.
*
* @param string $relation
* @param array $relations
* @return $this
*/
public function loadMorph($relation, $relations)
{
$this->pluck($relation)
->groupBy(function ($model) {
return get_class($model);
})
->filter(function ($models, $className) use ($relations) {
return Arr::has($relations, $className);
})
->each(function ($models, $className) use ($relations) {
$className::with($relations[$className])
->eagerLoadRelations($models->all());
});

return $this;
}

/**
* Add an item to the collection.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Pagination/AbstractPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,20 @@ public function setCollection(Collection $collection)
return $this;
}

/**
* Load a set of relationships onto the mixed relationship collection.
*
* @param string $relation
* @param array $relations
* @return $this
*/
public function loadMorph($relation, $relations)
{
$this->getCollection()->loadMorph($relation, $relations);

return $this;
}

/**
* Determine if the given item exists.
*
Expand Down
28 changes: 26 additions & 2 deletions tests/Database/DatabaseEloquentPolymorphicIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPUnit\Framework\TestCase;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;

Expand Down Expand Up @@ -119,6 +118,26 @@ public function testItLoadsNestedRelationshipsOnDemand()
$this->assertEquals(TestUser::first(), $like->likeable->owner);
}

public function testItLoadsNestedMorphRelationshipsOnDemand()
{
$this->seedData();

TestPost::first()->likes()->create([]);

$likes = TestLike::with('likeable.owner')->get()->loadMorph('likeable', [
TestComment::class => ['commentable'],
TestPost::class => 'comments',
]);

$this->assertTrue($likes[0]->relationLoaded('likeable'));
$this->assertTrue($likes[0]->likeable->relationLoaded('owner'));
$this->assertTrue($likes[0]->likeable->relationLoaded('commentable'));

$this->assertTrue($likes[1]->relationLoaded('likeable'));
$this->assertTrue($likes[1]->likeable->relationLoaded('owner'));
$this->assertTrue($likes[1]->likeable->relationLoaded('comments'));
}

/**
* Helpers...
*/
Expand All @@ -144,7 +163,7 @@ protected function connection()
/**
* Get a schema builder instance.
*
* @return Schema\Builder
* @return \Illuminate\Database\Schema\Builder
*/
protected function schema()
{
Expand Down Expand Up @@ -183,6 +202,11 @@ public function owner()
{
return $this->belongsTo(TestUser::class, 'user_id');
}

public function likes()
{
return $this->morphMany(TestLike::class, 'likeable');
}
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/Pagination/PaginatorLoadMorphTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Illuminate\Tests\Pagination;

use Mockery;
use PHPUnit\Framework\TestCase;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\AbstractPaginator;

class PaginatorLoadMorphTest extends TestCase
{
public function testCollectionLoadMorphCanChainOnThePaginator()
{
$relations = [
'App\\User' => 'photos',
'App\\Company' => ['employees', 'calendars'],
];

$items = Mockery::mock(Collection::class);
$items->shouldReceive('loadMorph')->once()->with('parentable', $relations);

$p = (new class extends AbstractPaginator {
})->setCollection($items);

$this->assertSame($p, $p->loadMorph('parentable', $relations));
}
}