I'm going to take a wild guess that your LeagueFactory class is in your factory folder, not your app folder? Maybe?
Or the class isn't bound to the service container?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm following this tutorial to create class based model factories.
I'm struggling to register the custom class with the app() helper.
Here is the custom model factory class:
// database/LeagueFactory.php
<?php
use App\League;
class LeagueFactory
{
public function create($overrides = [])
{
return factory(League::class)->create($overrides);
}
}
And here's how I want to call it within a test:
$league = app(LeagueFactory::class)->create([
'user_id' => $user->id,
'season_id' => $season->id,
]);
I only get this error message:
Illuminate\Contracts\Container\BindingResolutionException: Target class [StrikeFactory] does not exist
Where and how can I register the custom model factory, so I can globally access it?
Yes, @fylzero and @goldtaste, just referencing with a namespace solved this issue.
Just put my class file in app\FactoryClass and added namespace namespace App\FactoryClass and could import it easily with use App\FactoryClass\LeagueFactory and app(LeagueFactory::create()).
You might also use Realtime Facades to get rid of the app().
Please or to participate in this conversation.