Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

beli0135's avatar

Factory create records in cascade

Question is about user factory and automatic creation of child table records. So far, I have been creating usual way. Creating users then creating posts.

User::factory()
      ->has(Post::factory()->count(10))
      ->create();

Now... I have a different situation:

Users 
	- hasMany Roles
	- hasOne Profile which 
			- hasOne ProfileConfigs 
			- hasMany OtherConfigs

Can I make automatic creation of all downward records based on rules (which I guess is separate factories), but to be executed at once when user factory is started? How it is proper way to be done?

0 likes
1 reply

Please or to participate in this conversation.