Skip to content

Commit

Permalink
[7.x] Add 'loadMorphCount' method (#32739)
Browse files Browse the repository at this point in the history
* Add 'loadMorphCount' method to Eloquent Collection

* Add 'loadMorphCount' proxy method to AbstractPaginator

Co-authored-by: GuntherDebrauwer <22586858+GuntherDebrauwer@users.noreply.github.com>
  • Loading branch information
gdebrauwer and gdebrauwer committed May 11, 2020
1 parent 2d0ec75 commit ec8bf7e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ public function loadMorph($relation, $relations)
return $this;
}

/**
* Load a set of relationship counts onto the mixed relationship collection.
*
* @param string $relation
* @param array $relations
* @return $this
*/
public function loadMorphCount($relation, $relations)
{
$this->pluck($relation)
->filter()
->groupBy(function ($model) {
return get_class($model);
})
->each(function ($models, $className) use ($relations) {
static::make($models)->loadCount($relations[$className] ?? []);
});

return $this;
}

/**
* Determine if a key exists in 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 @@ -291,6 +291,20 @@ public function loadMorph($relation, $relations)
return $this;
}

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

return $this;
}

/**
* Get the slice of items being paginated.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Database/DatabaseEloquentPolymorphicIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,31 @@ public function testItLoadsNestedMorphRelationshipsOnDemand()
$this->assertTrue($likes[1]->likeable->relationLoaded('comments'));
}

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

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

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

$this->assertTrue($likes[0]->relationLoaded('likeable'));
$this->assertTrue($likes[0]->likeable->relationLoaded('owner'));
$this->assertEquals(2, $likes[0]->likeable->likes_count);

$this->assertTrue($likes[1]->relationLoaded('likeable'));
$this->assertTrue($likes[1]->likeable->relationLoaded('owner'));
$this->assertEquals(1, $likes[1]->likeable->comments_count);

$this->assertTrue($likes[2]->relationLoaded('likeable'));
$this->assertTrue($likes[2]->likeable->relationLoaded('owner'));
$this->assertEquals(2, $likes[2]->likeable->likes_count);
}

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

namespace Illuminate\Tests\Pagination;

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

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

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

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

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

0 comments on commit ec8bf7e

Please sign in to comment.