[9.x] Fix loadAggregate not correctly applying casts#41050
Merged
taylorotwell merged 4 commits intolaravel:9.xfrom Feb 16, 2022
ahme-d:8.x
Merged
[9.x] Fix loadAggregate not correctly applying casts#41050taylorotwell merged 4 commits intolaravel:9.xfrom ahme-d:8.x
taylorotwell merged 4 commits intolaravel:9.xfrom
ahme-d:8.x
Conversation
Member
|
Can you provide a simple example of how I can recreate this issue in a fresh Laravel application with code examples? Please mark this as ready for review when you have done so otherwise I won't see it. |
Contributor
Author
|
Hey @taylorotwell Here's a simple code example ( Also the PR contains a test which would fail on a fresh laravel installation ): Models class User extends Authenticatable
{
public function posts()
{
return $this->hasMany(Post::class);
}
}class Post extends Model
{
use HasFactory;
protected $guarded = [];
public function comments()
{
return $this->hasMany(Comment::class);
}
}class Comment extends Model
{
use HasFactory;
}Some data $user = \App\Models\User::find(1);
$user->posts()->createMany([
['content' => 'Test'],
['content' => 'Test'],
['content' => 'Test'],
]);The test $user = \App\Models\User::with('posts')->find(1);
$user->posts->loadExists("comments");
return $user->posts;This will result in the following ( notice and dd($user->posts->pluck('comments_exists')); |
Member
Contributor
Author
Member
|
Thanks |
GrahamCampbell
pushed a commit
that referenced
this pull request
Feb 18, 2022
* 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>
taylorotwell
added a commit
that referenced
this pull request
Feb 18, 2022
* 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.





Reopening #41025 with tests and better explanation about the bug.
There's a bug in
loadAggregatein Eloquent Collections specefically when runningLoadExistswhere running something like$user->articles->loadExists("comments")will addcomments_existsto all existing articles but only the first model will have boolcastsautomatically added to it. Resulting in something similar to:This PR fixes that and adds casts to all items instead of only the first one. I highly doubt it'll be breaking change to anyone.