Skip to content

Commit

Permalink
Add Eloquent Collection loadMorph()
Browse files Browse the repository at this point in the history
Allow nested relations to be eager loaded for
morphTo() relationships of mixed classes.

e.g.,

class ActivityFeed
{
    function parentable()
    {
        return $this->morphTo();
    }
}

$collection = ActivityFeed::with('parentable')
    ->get()
    ->loadMorph('parentable', [
        Event::class => 'calendar',
        Photo::class => 'tags',
        Post::class => ['author', 'commentsCount'],
    ]);

or

$paginator = ActivityFeed::with('parentable')
    ->paginate()
    ->loadMorph('parentable', [
        // etc.
    ]);
  • Loading branch information
derekmd authored and Derek MacDonald committed Mar 24, 2018
1 parent 3fd1b23 commit 046e004
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
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
26 changes: 26 additions & 0 deletions tests/Pagination/PaginatorLoadMorphTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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')->with('parentable', $relations);

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

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

0 comments on commit 046e004

Please sign in to comment.