Skip to content

Commit

Permalink
[8.x] Fix loadAggregate not correctly applying casts (#41050) (#41108)
Browse files Browse the repository at this point in the history
* 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 <taylor@laravel.com>

Co-authored-by: A. Alyusuf <ahmed@vioat.com>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
3 people committed Feb 18, 2022
1 parent 9236336 commit 60f9da3
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Eloquent/Collection.php
Expand Up @@ -92,7 +92,9 @@ public function loadAggregate($relations, $column, $function = null)
$this->each(function ($model) use ($models, $attributes) {
$extraAttributes = Arr::only($models->get($model->getKey())->getAttributes(), $attributes);

$model->forceFill($extraAttributes)->syncOriginalAttributes($attributes);
$model->forceFill($extraAttributes)
->syncOriginalAttributes($attributes)
->mergeCasts($models->get($model->getKey())->getCasts());
});

return $this;
Expand Down
124 changes: 124 additions & 0 deletions tests/Database/DatabaseEloquentCollectionTest.php
Expand Up @@ -2,9 +2,11 @@

namespace Illuminate\Tests\Database;

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Support\Collection as BaseCollection;
use LogicException;
use Mockery as m;
Expand All @@ -13,8 +15,51 @@

class DatabaseEloquentCollectionTest extends TestCase
{
/**
* Setup the database schema.
*
* @return void
*/
protected function setUp(): void
{
$db = new DB;

$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);

$db->bootEloquent();
$db->setAsGlobal();

$this->createSchema();
}

protected function createSchema()
{
$this->schema()->create('users', function ($table) {
$table->increments('id');
$table->string('email')->unique();
});

$this->schema()->create('articles', function ($table) {
$table->increments('id');
$table->integer('user_id');
$table->string('title');
});

$this->schema()->create('comments', function ($table) {
$table->increments('id');
$table->integer('article_id');
$table->string('content');
});
}

protected function tearDown(): void
{
$this->schema()->drop('users');
$this->schema()->drop('articles');
$this->schema()->drop('comments');
m::close();
}

Expand Down Expand Up @@ -536,6 +581,54 @@ public function testConvertingEmptyCollectionToQueryThrowsException()
$c = new Collection;
$c->toQuery();
}

public function testLoadExistsShouldCastBool()
{
$this->seedData();
$user = EloquentTestUserModel::with('articles')->first();
$user->articles->loadExists('comments');
$commentsExists = $user->articles->pluck('comments_exists')->toArray();
$this->assertContainsOnly('bool', $commentsExists);
}

/**
* Helpers...
*/
protected function seedData()
{
$user = EloquentTestUserModel::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);

EloquentTestArticleModel::query()->insert([
['user_id' => 1, 'title' => 'Another title'],
['user_id' => 1, 'title' => 'Another title'],
['user_id' => 1, 'title' => 'Another title'],
]);

EloquentTestCommentModel::query()->insert([
['article_id' => 1, 'content' => 'Another comment'],
['article_id' => 2, 'content' => 'Another comment'],
]);
}

/**
* Get a database connection instance.
*
* @return \Illuminate\Database\ConnectionInterface
*/
protected function connection()
{
return Eloquent::getConnectionResolver()->connection();
}

/**
* Get a schema builder instance.
*
* @return \Illuminate\Database\Schema\Builder
*/
protected function schema()
{
return $this->connection()->getSchemaBuilder();
}
}

class TestEloquentCollectionModel extends Model
Expand All @@ -548,3 +641,34 @@ public function getTestAttribute()
return 'test';
}
}

class EloquentTestUserModel extends Model
{
protected $table = 'users';
protected $guarded = [];
public $timestamps = false;

public function articles()
{
return $this->hasMany(EloquentTestArticleModel::class, 'user_id');
}
}

class EloquentTestArticleModel extends Model
{
protected $table = 'articles';
protected $guarded = [];
public $timestamps = false;

public function comments()
{
return $this->hasMany(EloquentTestCommentModel::class, 'article_id');
}
}

class EloquentTestCommentModel extends Model
{
protected $table = 'comments';
protected $guarded = [];
public $timestamps = false;
}

0 comments on commit 60f9da3

Please sign in to comment.