Skip to content

Commit 9d788a0

Browse files
authored
Adds define() method to ModelFactory (#5897)
* Adds `define()` method to `ModelFactory` * Update CHANGELOG-3.1.md
1 parent 4cb1568 commit 9d788a0

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/Concerns/InteractsWithModelFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ trait InteractsWithModelFactory
1818
{
1919
protected ?ModelFactory $modelFactory = null;
2020

21-
protected string|array $factoryPath = BASE_PATH . '/database/factories';
21+
/**
22+
* @var string|string[]
23+
*/
24+
protected $factoryPath = BASE_PATH . '/database/factories';
2225

2326
protected function setUpInteractsWithModelFactory()
2427
{

src/ModelFactory.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,27 @@ public static function create(Generator $faker)
2828
return new static(new Factory($faker));
2929
}
3030

31+
public function define(string $class, callable $attributes, string $name = 'default'): void
32+
{
33+
$this->factory->define(...func_get_args());
34+
}
35+
3136
/**
32-
* @param class-string<T> $model
37+
* @param class-string<T> $class
3338
* @return T
3439
*/
35-
public function factory(string $model, ...$arguments)
40+
public function factory(string $class)
3641
{
37-
$factory = $this->factory;
42+
$arguments = func_get_args();
3843

3944
if (isset($arguments[1]) && is_string($arguments[1])) {
40-
return $factory->of($arguments[0], $arguments[1])->times($arguments[2] ?? null);
45+
return $this->factory->of($arguments[0], $arguments[1])->times($arguments[2] ?? null);
4146
}
4247
if (isset($arguments[1])) {
43-
return $factory->of($arguments[0])->times($arguments[1]);
48+
return $this->factory->of($arguments[0])->times($arguments[1]);
4449
}
4550

46-
return $factory->of($arguments[0]);
51+
return $this->factory->of($arguments[0]);
4752
}
4853

4954
public function load(string $path): void

0 commit comments

Comments
 (0)