scribe icon indicating copy to clipboard operation
scribe copied to clipboard

`ResponseFromApiResource` `with` parameter with model that doesn't have a factory

Open toyi opened this issue 1 year ago • 4 comments

Scribe version

5.1.0

Your question

When I generate the documentation with an attribute like this:

#[ResponseFromApiResource(UserResource::class, model: User::class, collection: true, with: ['gender'])]

It prints this warning and doesn't genere the response:

⚠ Couldn't get example model for App\Models\User via factoryCreate.
⚠ Couldn't get example model for App\Models\User via factoryMake.
⚠ Couldn't get example model for App\Models\User via factoryCreate.
⚠ Couldn't get example model for App\Models\User via factoryMake.

After some diggings, it seems the underlying exception Call to undefined method App\Models\Gender::factory() is thrown because the GenderFactory doesn't exist, which is normal in my case since it's a "static" table, values are populated in a migration.

I think we could try to grab the first db row if there is no factory attached to a model before throwing an error.

I naively tried to declare a factory function inside the Gender model to mimic the factory and return the first row and it worked well, but I don't know if it would be the case in every circumstances:

protected static function factory(): ?static
{
    return static::first();
}

Is it worth a PR or am I missing something more built-in to handle this specific case?

Docs

toyi avatar Apr 03 '25 14:04 toyi