Skip to content

Commit 60f9da3

Browse files
GrahamCampbellahme-dtaylorotwell
authored
[8.x] Fix loadAggregate not correctly applying casts (#41050) (#41108)
* Fix loadAggregate not correctly applying casts * Added tests to "Fix loadAggregate not correctly applying casts" * Style fix * Update Collection.php Co-authored-by: Taylor Otwell <[email protected]> Co-authored-by: A. Alyusuf <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 9236336 commit 60f9da3

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

src/Illuminate/Database/Eloquent/Collection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public function loadAggregate($relations, $column, $function = null)
9292
$this->each(function ($model) use ($models, $attributes) {
9393
$extraAttributes = Arr::only($models->get($model->getKey())->getAttributes(), $attributes);
9494

95-
$model->forceFill($extraAttributes)->syncOriginalAttributes($attributes);
95+
$model->forceFill($extraAttributes)
96+
->syncOriginalAttributes($attributes)
97+
->mergeCasts($models->get($model->getKey())->getCasts());
9698
});
9799

98100
return $this;

tests/Database/DatabaseEloquentCollectionTest.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Illuminate\Tests\Database;
44

5+
use Illuminate\Database\Capsule\Manager as DB;
56
use Illuminate\Database\Eloquent\Builder;
67
use Illuminate\Database\Eloquent\Collection;
78
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\Model as Eloquent;
810
use Illuminate\Support\Collection as BaseCollection;
911
use LogicException;
1012
use Mockery as m;
@@ -13,8 +15,51 @@
1315

1416
class DatabaseEloquentCollectionTest extends TestCase
1517
{
18+
/**
19+
* Setup the database schema.
20+
*
21+
* @return void
22+
*/
23+
protected function setUp(): void
24+
{
25+
$db = new DB;
26+
27+
$db->addConnection([
28+
'driver' => 'sqlite',
29+
'database' => ':memory:',
30+
]);
31+
32+
$db->bootEloquent();
33+
$db->setAsGlobal();
34+
35+
$this->createSchema();
36+
}
37+
38+
protected function createSchema()
39+
{
40+
$this->schema()->create('users', function ($table) {
41+
$table->increments('id');
42+
$table->string('email')->unique();
43+
});
44+
45+
$this->schema()->create('articles', function ($table) {
46+
$table->increments('id');
47+
$table->integer('user_id');
48+
$table->string('title');
49+
});
50+
51+
$this->schema()->create('comments', function ($table) {
52+
$table->increments('id');
53+
$table->integer('article_id');
54+
$table->string('content');
55+
});
56+
}
57+
1658
protected function tearDown(): void
1759
{
60+
$this->schema()->drop('users');
61+
$this->schema()->drop('articles');
62+
$this->schema()->drop('comments');
1863
m::close();
1964
}
2065

@@ -536,6 +581,54 @@ public function testConvertingEmptyCollectionToQueryThrowsException()
536581
$c = new Collection;
537582
$c->toQuery();
538583
}
584+
585+
public function testLoadExistsShouldCastBool()
586+
{
587+
$this->seedData();
588+
$user = EloquentTestUserModel::with('articles')->first();
589+
$user->articles->loadExists('comments');
590+
$commentsExists = $user->articles->pluck('comments_exists')->toArray();
591+
$this->assertContainsOnly('bool', $commentsExists);
592+
}
593+
594+
/**
595+
* Helpers...
596+
*/
597+
protected function seedData()
598+
{
599+
$user = EloquentTestUserModel::create(['id' => 1, 'email' => '[email protected]']);
600+
601+
EloquentTestArticleModel::query()->insert([
602+
['user_id' => 1, 'title' => 'Another title'],
603+
['user_id' => 1, 'title' => 'Another title'],
604+
['user_id' => 1, 'title' => 'Another title'],
605+
]);
606+
607+
EloquentTestCommentModel::query()->insert([
608+
['article_id' => 1, 'content' => 'Another comment'],
609+
['article_id' => 2, 'content' => 'Another comment'],
610+
]);
611+
}
612+
613+
/**
614+
* Get a database connection instance.
615+
*
616+
* @return \Illuminate\Database\ConnectionInterface
617+
*/
618+
protected function connection()
619+
{
620+
return Eloquent::getConnectionResolver()->connection();
621+
}
622+
623+
/**
624+
* Get a schema builder instance.
625+
*
626+
* @return \Illuminate\Database\Schema\Builder
627+
*/
628+
protected function schema()
629+
{
630+
return $this->connection()->getSchemaBuilder();
631+
}
539632
}
540633

541634
class TestEloquentCollectionModel extends Model
@@ -548,3 +641,34 @@ public function getTestAttribute()
548641
return 'test';
549642
}
550643
}
644+
645+
class EloquentTestUserModel extends Model
646+
{
647+
protected $table = 'users';
648+
protected $guarded = [];
649+
public $timestamps = false;
650+
651+
public function articles()
652+
{
653+
return $this->hasMany(EloquentTestArticleModel::class, 'user_id');
654+
}
655+
}
656+
657+
class EloquentTestArticleModel extends Model
658+
{
659+
protected $table = 'articles';
660+
protected $guarded = [];
661+
public $timestamps = false;
662+
663+
public function comments()
664+
{
665+
return $this->hasMany(EloquentTestCommentModel::class, 'article_id');
666+
}
667+
}
668+
669+
class EloquentTestCommentModel extends Model
670+
{
671+
protected $table = 'comments';
672+
protected $guarded = [];
673+
public $timestamps = false;
674+
}

0 commit comments

Comments
 (0)