Skip to content
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

[11.x] Fix attribute mutator access in loadMissing #53879

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
@@ -271,7 +271,7 @@ protected function loadMissingRelation(self $models, array $path)
return;
}

$models = $models->pluck($name)->whereNotNull();
$models = $models->pluck($name)->filter();

if ($models->first() instanceof BaseCollection) {
$models = $models->collapse();
62 changes: 61 additions & 1 deletion tests/Integration/Database/EloquentModelLoadMissingTest.php
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
namespace Illuminate\Tests\Integration\Database\EloquentModelLoadMissingTest;

use DB;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@@ -12,20 +13,40 @@ class EloquentModelLoadMissingTest extends DatabaseTestCase
{
protected function afterRefreshingDatabase()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});

Schema::create('comment_mentions_users', function (Blueprint $table) {
$table->unsignedInteger('comment_id');
$table->unsignedInteger('user_id');
});

Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('first_comment_id')->nullable();
$table->string('content')->nullable();
});

Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('parent_id')->nullable();
$table->unsignedInteger('post_id');
$table->string('content')->nullable();
});

Post::create();

Comment::create(['parent_id' => null, 'post_id' => 1]);
Comment::create(['parent_id' => null, 'post_id' => 1, 'content' => 'Hello <u:1> <u:2>']);
Comment::create(['parent_id' => 1, 'post_id' => 1]);

User::create(['name' => 'Taylor']);
User::create(['name' => 'Otwell']);

Comment::first()->mentionsUsers()->attach([1, 2]);

Post::first()->update(['first_comment_id' => 1]);
}

public function testLoadMissing()
@@ -39,6 +60,17 @@ public function testLoadMissing()
$this->assertCount(1, DB::getQueryLog());
$this->assertTrue($post->comments[0]->relationLoaded('parent'));
}

public function testLoadMissingNoUnnecessaryAttributeMutatorAccess()
{
$posts = Post::all();

DB::enableQueryLog();

$posts->loadMissing('firstComment.parent');

$this->assertCount(1, DB::getQueryLog());
}
}

class Comment extends Model
@@ -51,14 +83,42 @@ public function parent()
{
return $this->belongsTo(self::class);
}

public function mentionsUsers()
{
return $this->belongsToMany(User::class, 'comment_mentions_users');
}

public function content(): Attribute
{
return new Attribute(function (?string $value) {
return preg_replace_callback('/<u:(\d+)>/', function ($matches) {
return '@'.$this->mentionsUsers->find($matches[1])?->name;
}, $value);
});
}
}

class Post extends Model
{
public $timestamps = false;

protected $guarded = [];

public function comments()
{
return $this->hasMany(Comment::class);
}

public function firstComment()
{
return $this->belongsTo(Comment::class, 'first_comment_id');
}
}

class User extends Model
{
public $timestamps = false;

protected $guarded = [];
}