nolanos/laravel-doctrine-factory
Install the Package
composer require stemble/laravel-doctrine-factory
Add the Trait
Annotate your Doctrine entity with @ORM\Entity and add the HasFactory trait:
use Doctrine\ORM\Mapping as ORM;
use Stemble\DoctrineFactory\HasFactory;
#[ORM\Entity]
class User
{
use HasFactory;
// ...
}
Generate a Factory
Create a factory class in database/factories (e.g., UserFactory.php):
namespace Database\Factories;
use Stemble\DoctrineFactory\Factory;
class UserFactory extends Factory
{
protected $model = User::class;
public function definition()
{
return [
'name' => 'John Doe',
'email' => 'john@example.com',
];
}
}
First Use Case Create a user in tests or migrations:
use Database\Factories\UserFactory;
$user = UserFactory::new()->create();
Basic Creation
// Single entity
$user = UserFactory::new()->create();
// Multiple entities
$users = UserFactory::new()->count(5)->create();
State Management Define states in the factory:
class UserFactory extends Factory
{
public function admin()
{
return $this->state([
'role' => 'admin',
'is_active' => true,
]);
}
}
Usage:
$admin = UserFactory::new()->admin()->create();
Relationships
Use has() and for() helpers:
class PostFactory extends Factory
{
public function definition()
{
return ['title' => 'Test Post'];
}
}
// Create a user with posts
$user = UserFactory::new()
->has(Post::class, 3)
->create();
Sequences Generate sequential data:
class UserFactory extends Factory
{
protected static function definition()
{
return [
'email' => 'user_' . self::$sequence . '@example.com',
];
}
}
Callbacks
Use afterMaking and afterCreating:
class UserFactory extends Factory
{
protected function afterMaking()
{
$this->sequence('email');
}
}
Entity Manager Binding Ensure the Doctrine Entity Manager is bound in the service container:
$this->app->bind(\Doctrine\ORM\EntityManagerInterface::class, function ($app) {
return $app->make('doctrine.orm.entity_manager');
});
Transaction Handling Factories create entities in a transaction by default. For bulk operations, disable transactions:
$users = UserFactory::new()->count(100)->disableTransactions()->create();
Sequence Conflicts Avoid reusing sequences across unrelated factories to prevent ID collisions.
Missing HasFactory Trait
Forgetting to add use Stemble\DoctrineFactory\HasFactory; will cause runtime errors.
database/factories and follows naming conventions (UserFactory for User entity).has() and for() relationships reference valid factory classes.database/factories. Customize via:
'factories' => [
'paths' => [app_path('Factories')],
],
in config/doctrine-factory.php.Custom States
Dynamically define states via state() or predefine them in the factory class.
Factory Macros Add reusable logic:
UserFactory::macro('withRole', function ($role) {
return $this->state(['role' => $role]);
});
Usage:
$user = UserFactory::new()->withRole('editor')->create();
Override Defaults
Use afterMaking to modify attributes post-creation:
protected function afterMaking()
{
$this->model->setPassword(Hash::make('password'));
}
Custom Model Binding
Override $model in the factory for polymorphic entities:
protected $model = function () {
return User::class; // or dynamic logic
};
How can I help you explore Laravel packages today?