Summer Sale! All accounts are 50% off this week.

learningMonkey's avatar

factory error

This is the factory code


namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Project>
 */
class ProjectFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'title' => $faker->sentence,
            'description' => $faker->paragraph
        ];
    }
}

when i run this with tinker ,

 factory('App\Models\project')->make()
PHP Error:  Call to undefined function factory() in C:\laragon\www\birdboard-testeval()'d code on line 1

0 likes
8 replies
automica's avatar

@jaspal ensure you are using the ‘UseFactory’ trait in the model you’ve created the factory for

learningMonkey's avatar

@frankielee now it showing this error

 factory('App\Models\Project')->make()
InvalidArgumentException with message 'Unable to locate factory for [App\Models\Project].'
MohamedTammam's avatar
Level 51

Convert your factory to be

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Project>
 */
class ProjectFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'title' => fake()->sentence,
            'description' => fake()->paragraph
        ];
    }
}

And call it like

App\Models\Project::factory()->make()
1 like

Please or to participate in this conversation.