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

jbianchi's avatar

Closure on factory

I'm trying to use closures on factories like laravel docs:

/**
 * Define the model's default state.
 *
 * @return array
 */
public function definition()
{
    return [
        'user_id' => User::factory(),
        'user_type' => function (array $attributes) {
            return User::find($attributes['user_id'])->type;
        },
        'title' => $this->faker->title(),
        'content' => $this->faker->paragraph(),
    ];
}

But when I try to run a seeder that calls the factory I get "Object of class Closure could not be converted to string".

This feature isn't available at Lumen? I'm using Lumen 8.2.3

0 likes
3 replies
Daniel-Pablo's avatar

where did you get this code?

factories like laravel docs: can you post the link to these Laravel docs, please

jbianchi's avatar

I can't post links in my first day here:

laravel.com /docs/8.x/ database-testing#defining-relationships-within-factories

jbianchi's avatar

I tried something simple just for test, like:

'user_id' => fn() => 1

Same error

Please or to participate in this conversation.