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

joeygreco's avatar

Laravel 8 - Using Faker and Factory

I created a factory for a created class "Article". The provided user factory is working fine. Running Article::factory()->create() creates a new user in the db, but it appears that the faker values are returning null and preventing me from creating a new article as well.

This error is returning in terminal.

PHP Notice: Undefined variable: faker in /firstproject/database/factories/ArticleFactory.php on line 29 PHP Notice: Trying to get property 'sentence' of non-object in /firstproject/database/factories/ArticleFactory.php on line 29 PHP Notice: Undefined variable: faker in /firstproject/database/factories/ArticleFactory.php on line 30 PHP Notice: Trying to get property 'sentence' of non-object in /firstproject/database/factories/ArticleFactory.php on line 30 PHP Notice: Undefined variable: faker in /firstproject/database/factories/ArticleFactory.php on line 31 PHP Notice: Trying to get property 'paragraph' of non-object in /firstproject/database/factories/ArticleFactory.php on line 31

Here is my code:

<?php

namespace Database\Factories;

use App\Models\Article;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;



class ArticleFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Article::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'user_id' => User::factory(),
            'title' => $this->faker->sentence,
            'excerpt' => $this->faker->sentence,
            'body' => $this->faker->paragraph,
        ];
    }
}
0 likes
7 replies
siangboon's avatar

just refer to the original example and compare what is missing....

Laravel 7.24:

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/

$factory->define(User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'email_verified_at' => now(),
        'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        'remember_token' => Str::random(10),
    ];
});


2 likes
joeygreco's avatar

Here is user factory which is working for me. The lesson I'm following is laravel 6 and I am using laravel 8. Im struggling to find a difference if any that I am missing.

<?php

namespace Database\Factories;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class UserFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = User::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => $this->faker->name,
            'email' => $this->faker->unique()->safeEmail,
            'email_verified_at' => now(),
            'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    }
}

georgekunchattil's avatar

@joeygreco If we need to declare another function with class (say class 'Post'), how can we declare function from 'database/factories/UserFactory.php' using Laravel 8 ?

georgekunchattil's avatar

@Sinnbeck Here is the code. I think to declare this is the way, we declare another function for another class using Laravel 8

alt text alt text alt text

Please or to participate in this conversation.