Skip to content

[12.x] Resolve issue with Factory make when automatic eager loading #56211

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

Merged
merged 3 commits into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
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
40 changes: 25 additions & 15 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,27 +394,37 @@ public function makeOne($attributes = [])
*/
public function make($attributes = [], ?Model $parent = null)
{
if (! empty($attributes)) {
return $this->state($attributes)->make([], $parent);
}
$autoEagerLoadingEnabled = Model::isAutomaticallyEagerLoadingRelationships();

if ($this->count === null) {
return tap($this->makeInstance($parent), function ($instance) {
$this->callAfterMaking(new Collection([$instance]));
});
if ($autoEagerLoadingEnabled) {
Model::automaticallyEagerLoadRelationships(false);
}

if ($this->count < 1) {
return $this->newModel()->newCollection();
}
try {
if (! empty($attributes)) {
return $this->state($attributes)->make([], $parent);
}

if ($this->count === null) {
return tap($this->makeInstance($parent), function ($instance) {
$this->callAfterMaking(new Collection([$instance]));
});
}

$instances = $this->newModel()->newCollection(array_map(function () use ($parent) {
return $this->makeInstance($parent);
}, range(1, $this->count)));
if ($this->count < 1) {
return $this->newModel()->newCollection();
}

$instances = $this->newModel()->newCollection(array_map(function () use ($parent) {
return $this->makeInstance($parent);
}, range(1, $this->count)));

$this->callAfterMaking($instances);
$this->callAfterMaking($instances);

return $instances;
return $instances;
} finally {
Model::automaticallyEagerLoadRelationships($autoEagerLoadingEnabled);
}
}

/**
Expand Down
76 changes: 76 additions & 0 deletions tests/Integration/Database/EloquentModelRelationAutoloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Tests\Integration\Database\EloquentModelRelationAutoloadTest;

use DB;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand All @@ -12,6 +14,13 @@ class EloquentModelRelationAutoloadTest extends DatabaseTestCase
{
protected function afterRefreshingDatabase()
{
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->nullable();
$table->string('status')->nullable();
$table->unsignedInteger('post_id')->nullable();
});

Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
});
Expand Down Expand Up @@ -214,6 +223,63 @@ public function testRelationAutoloadVariousNestedMorphRelations()

DB::disableQueryLog();
}

public function testRelationAutoloadWorksOnFactoryMake()
{
Model::automaticallyEagerLoadRelationships();

DB::enableQueryLog();

$tags = Tag::factory()->times(3)->make();

$post = Post::create();

$post->tags()->saveMany($tags);

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

Model::automaticallyEagerLoadRelationships(false);

DB::disableQueryLog();
}
}

class TagFactory extends Factory
{
protected $model = Tag::class;

public function definition()
{
return [];
}
}

class Tag extends Model
{
use HasFactory;

public $timestamps = false;

protected $guarded = [];

protected static function booted()
{
static::creating(function ($model) {
if ($model->post->shouldApplyStatus()) {
$model->status = 'Todo';
}
});
}

protected static function newFactory()
{
return TagFactory::new();
}

public function post()
{
return $this->belongsTo(Post::class);
}
}

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

public function shouldApplyStatus()
{
return false;
}

public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
Expand All @@ -256,6 +327,11 @@ public function likes()
{
return $this->morphMany(Like::class, 'likeable');
}

public function tags()
{
return $this->hasMany(Tag::class);
}
}

class Video extends Model
Expand Down