diff --git a/src/Illuminate/Database/Eloquent/Factories/Factory.php b/src/Illuminate/Database/Eloquent/Factories/Factory.php index 30607e7c45fc..6c0cf94ba77c 100644 --- a/src/Illuminate/Database/Eloquent/Factories/Factory.php +++ b/src/Illuminate/Database/Eloquent/Factories/Factory.php @@ -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); + } } /** diff --git a/tests/Integration/Database/EloquentModelRelationAutoloadTest.php b/tests/Integration/Database/EloquentModelRelationAutoloadTest.php index 03f1d7ca611f..f7eb6dbee637 100644 --- a/tests/Integration/Database/EloquentModelRelationAutoloadTest.php +++ b/tests/Integration/Database/EloquentModelRelationAutoloadTest.php @@ -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; @@ -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'); }); @@ -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 @@ -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'); @@ -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