xtremer360
2 months ago
318
3
Laravel

Class Based Factories

Posted 2 months ago by xtremer360

I'm working with Christoph Rumpel's Laravel-Factories-Reloaded package which is a class based factories package. Inside of my TeamFactory class I am trying to create 9 players that will also have the playable method attached to each of them. Each player needs to have the same activationFactory applied as the team has.

$team = TeamFactory::new()->playable()->create();

TeamFactory


public function playable(ActivationFactory $activationFactory = null): TeamFactory
    {
        $clone = tap(clone $this)->overwriteDefaults([
            'status' => TeamStatus::PLAYABLE,
        ]);

        $clone = $clone->activated($activationFactory ?? $this->activationFactory);

        $clone->playerFactory = PlayerFactory::new()
            ->playable($activationFactory ?? $this->activationFactory)
            ->times(9);

        return $clone;
    }

public function activated(ActivationFactory $activationFactory = null)
    {
        $clone = clone $this;

        $clone->activationFactory = $activationFactory ?? ActivationFactory::new();

        return $clone;
    }

Please sign in or create an account to participate in this conversation.