Skip to content

Commit a91b059

Browse files
[12.x] Factory@insert() (#57600)
* Factory@insert() * clean up * Update Factory.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 3968c4d commit a91b059

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/Illuminate/Database/Eloquent/Factories/Factory.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,34 @@ public function make($attributes = [], ?Model $parent = null)
447447
}
448448
}
449449

450+
/**
451+
* Insert the model records in bulk. No model events are emitted.
452+
*
453+
* @param array<string, mixed> $attributes
454+
* @param Model|null $parent
455+
* @return void
456+
*/
457+
public function insert(array $attributes = [], ?Model $parent = null): void
458+
{
459+
$made = $this->make($attributes, $parent);
460+
461+
$madeCollection = $made instanceof Collection
462+
? $made
463+
: $this->newModel()->newCollection([$made]);
464+
465+
$model = $madeCollection->first();
466+
467+
if (isset($this->connection)) {
468+
$model->setConnection($this->connection);
469+
}
470+
471+
$query = $model->newQueryWithoutScopes();
472+
473+
$query->fillAndInsert(
474+
$madeCollection->withoutAppends()->toArray()
475+
);
476+
}
477+
450478
/**
451479
* Make an instance of the model with the given attributes.
452480
*

tests/Database/DatabaseEloquentFactoryTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Contracts\Foundation\Application;
1010
use Illuminate\Database\Capsule\Manager as DB;
1111
use Illuminate\Database\Eloquent\Attributes\UseFactory;
12+
use Illuminate\Database\Eloquent\Casts\Attribute;
1213
use Illuminate\Database\Eloquent\Collection;
1314
use Illuminate\Database\Eloquent\Factories\CrossJoinSequence;
1415
use Illuminate\Database\Eloquent\Factories\Factory;
@@ -982,6 +983,23 @@ public function test_factory_model_morph_many_relationship_has_pending_attribute
982983
$this->assertEquals('other body', FactoryTestComment::first()->body);
983984
}
984985

986+
public function test_factory_can_insert()
987+
{
988+
(new FactoryTestPostFactory())
989+
->count(5)
990+
->recycle([
991+
(new FactoryTestUserFactory())->create(['name' => Name::Taylor]),
992+
(new FactoryTestUserFactory())->create(['name' => Name::Shad, 'created_at' => now()])
993+
])
994+
->state(['title' => 'hello'])
995+
->insert();
996+
$this->assertCount(5, $posts = FactoryTestPost::query()->where('title', 'hello')->get());
997+
$this->assertEquals(strtoupper($posts[0]->user->name), $posts[0]->upper_case_name);
998+
$this->assertEquals(2, ($users = FactoryTestUser::query()->get())->count());
999+
$this->assertCount(1, $users->where('name', 'shaedrich'));
1000+
$this->assertCount(1, $users->where('name', 'totwell'));
1001+
}
1002+
9851003
/**
9861004
* Get a database connection instance.
9871005
*
@@ -1072,6 +1090,13 @@ class FactoryTestPost extends Eloquent
10721090

10731091
protected $table = 'posts';
10741092

1093+
protected $appends = ['upper_case_name'];
1094+
1095+
public function upperCaseName(): Attribute
1096+
{
1097+
return Attribute::get(fn ($attr) => Str::upper($this->user->name));
1098+
}
1099+
10751100
public function user()
10761101
{
10771102
return $this->belongsTo(FactoryTestUser::class, 'user_id');
@@ -1193,3 +1218,9 @@ class FactoryTestUseFactoryAttribute extends Eloquent
11931218
{
11941219
use HasFactory;
11951220
}
1221+
1222+
enum Name: string
1223+
{
1224+
case Taylor = 'totwell';
1225+
case Shad = 'shaedrich';
1226+
}

0 commit comments

Comments
 (0)