Skip to content

Commit ce3d25c

Browse files
[9.x] Add ->set() method to factory (#41890)
* Add ->set() method to factory * Formatting * Update Factory.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent e03e904 commit ce3d25c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,18 @@ public function state($state)
483483
]);
484484
}
485485

486+
/**
487+
* Set a single model attribute.
488+
*
489+
* @param string|int $key
490+
* @param mixed $value
491+
* @return static
492+
*/
493+
public function set($key, $value)
494+
{
495+
return $this->state([$key => $value]);
496+
}
497+
486498
/**
487499
* Add a new sequenced state transformation to the model definition.
488500
*

tests/Database/DatabaseEloquentFactoryTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ public function test_basic_model_can_be_created()
108108
$this->assertInstanceOf(Eloquent::class, $user);
109109
$this->assertSame('Taylor Otwell', $user->name);
110110

111+
$user = FactoryTestUserFactory::new()->set('name', 'Taylor Otwell')->create();
112+
$this->assertInstanceOf(Eloquent::class, $user);
113+
$this->assertSame('Taylor Otwell', $user->name);
114+
111115
$users = FactoryTestUserFactory::new()->createMany([
112116
['name' => 'Taylor Otwell'],
113117
['name' => 'Jeffrey Way'],

0 commit comments

Comments
 (0)