Skip to content

[12.x] Add Support for Selective Relationship Autoloading in withRelationshipAutoloading #55673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Prev Previous commit
Next Next commit
fix: test is fixed by removing unnecessary columns added
  • Loading branch information
moe-mizrak committed May 7, 2025
commit 294228dd2e0dc83943c20de4a9d80f66323279ba
38 changes: 6 additions & 32 deletions tests/Integration/Database/EloquentModelRelationAutoloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,49 +92,28 @@ public function testRelationAutoloadForSingleModel()

public function testWithRelationshipAutoloadingSelectiveColumns()
{
// Setup - create a post with comments and likes
$post = Post::create(['title' => 'Test Post', 'content' => 'Full content here']);
$post = Post::create();
$comment1 = $post->comments()->create([
'parent_id' => null,
'content' => 'First comment content',
'is_approved' => true
]);
$comment2 = $post->comments()->create([
'parent_id' => $comment1->id,
'content' => 'Second comment content',
'is_approved' => true
]);
$comment2->likes()->create(['user_id' => 1]);
$comment2->likes()->create(['user_id' => 2]);
$comment2->likes()->create();
$comment2->likes()->create();

DB::enableQueryLog();

$post = Post::find($post->id);

$post->withRelationshipAutoloading([
'comments:id,parent_id',
'likes:id,user_id'
'comments:id',
'comments.likes',
]);

$likes = [];

foreach ($post->comments as $comment) {
$likes = array_merge($likes, $comment->likes->all());
}

// Assertions
$this->assertCount(2, DB::getQueryLog());
$this->assertCount(2, $likes);

// Verify selective loading worked - these columns should be loaded
$this->assertNotNull($post->comments[0]->id);
$this->assertNotNull($post->comments[0]->parent_id);
$this->assertNotNull($likes[0]->user_id);

// These columns should NOT be loaded (will return null)
$this->assertNull($post->comments[0]->content);
$this->assertNull($post->comments[0]->is_approved);

$this->assertFalse(array_key_exists('parent_id', $post->comments[0]->getAttributes()));
$this->assertTrue($post->relationLoaded('comments'));
$this->assertTrue($post->comments[0]->relationLoaded('likes'));

Expand Down Expand Up @@ -293,11 +272,6 @@ class Post extends Model
{
public $timestamps = false;

protected $fillable = [
'title',
'content',
];

public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy