K
K
kosmok2020-09-11 21:04:32
Laravel
kosmok, 2020-09-11 21:04:32

$factory->define?

I have an error when creating factories (laravel 8) here is the code

$factory->define(\App\Models\BlogPost::class, function(Faker $faker)){

    $title = $faker->create()->sentence(rand(3,8),true);
    $txt = $faker->create()->realText(rand(1000,4000));
    $isPublished = rand(1,5) > 1;
    
    $data = [
        'category_id' => rand(1,10),
        'user_id' => (rand(1,5) == 5) ? 1 : 2,
        'title' => $title,
        'slug'  => Str::of($title)->slug(),
        'excerpt' => $faker->create()->text(rand(40,100)),
        'content_raw' => $txt,
        'content_html' => $txt,
        'is_published' => $isPublished,
        'published_at' => $isPublished ? $faker->create()->dateTimeBetween('-2 months','-1 days'):null,
        'created_at' => $faker->create()->dateTimeBetween('-3 months','-2 months'),
        'updated_at' => $faker->create()->dateTimeBetween('-3 months','-2 months'),
        
    ];
    
    return $data;
}

error in $factory->define(\App\Models\BlogPost::class, function(Faker $faker))

here are errors
1 C:\Users\kosmok\Desktop\OpenServer\domains\laravel\database\factories\BlogPostFactory.php :11
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined variable: factory", "C:\Users\kosmok\Desktop\OpenServer\domains\laravel\database\factories\BlogPostFactory.php", ["C:\ Users\kosmok\Desktop\OpenServer\domains\laravel\vendor\composer/../../database/factories\BlogPostFactory.php"])

2 C:\Users\kosmok\Desktop\OpenServer\domains\laravel\vendor\ composer\ClassLoader.php:444
include("C:\Users\kosmok\Desktop\OpenServer\domains\laravel\database\factories\BlogPostFactory.php")

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question