Option 1
In your simplified example there is no need for the afterCreating() or template() methods:
// DatabaseSeeder.php
Company::factory(300)
->has(Branch::factory()
->hasCarParks(['name' => 'Car Park 1'])
->hasCarParks(['name' => 'Car Park 2'])
->hasCarParks(['name' => 'Car Park 3'])
->hasCarParks(['name' => 'Car Park 4'])
)
->create();
Option 2
If you need the template() method:
// DatabaseSeeder.php
Company::factory(300)
->has(Branch::factory()
->has(CarPark::factory()->template()->state(['name' => 'Car Park 1']))
->has(CarPark::factory()->template()->state(['name' => 'Car Park 2']))
->has(CarPark::factory()->template()->state(['name' => 'Car Park 3']))
->has(CarPark::factory()->template()->state(['name' => 'Car Park 4']))
)
->create();
Option 3 - The Fancy Pants Way
You could also do something like this. But you would end up with Car Park 1, ..., Car Park 1200 in your database. Which may or may not be desirable.
// DatabaseSeeder.php
Company::factory(300)
->has(Branch::factory()->hasCarParks(4))
->create();
// CarParkFactory.php
public function definition()
{
static $id = 1;
return [
'branch_id' => Branch::factory(),
'name' => sprintf('Car Park %s', $id++),
];
}