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

andreigirnet96's avatar

PHP Fatal error: Call to undefined function factory() in Psy Shell code on line 1

PHP Fatal error: Call to undefined function factory() in Psy Shell code on line 1 Guys, please help, after 3 days of researching this error on the internet I haven't found any answer, I guess it is related to the fact that I am watching Jeffrey's Laravel 6 tutorials, and I am using 8 at the moment. The issue appeared while trying to create fake test data for the database using this in Laravel Tinker Articless:factory()->count(10)->create();. I understand it is not able to connect with the model, but I don't know why as I followed every little thing. PS it works fine for the User, I can create test user data Here is the code:

0 likes
29 replies
tykus's avatar

What is the answer to this question Does the model use the HasFactory trait?

And what is the name of the Factory?

1 like
andreigirnet96's avatar

The answer is Yes, it uses Has Factory; The name of the factory is ArticleFactory.php.

andreigirnet96's avatar

I have the Has Factory link

class Articless extends Model { use HasFactory; protected $guarded = []; public function path(){ return route('articles',$this); } public function user(){ return $this->belongsTo(User::class); }

}

lemmon's avatar
class Articless extends Model {

	use HasFactory;

	protected $guarded = [];

	public function path()
	{
		return route('articles',$this);
	}

	public function user()
	{
		return $this->belongsTo(User::class);
	}

}
andreigirnet96's avatar
namespace Database\Factories;

use App\Models\Articless;
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 = Articless::class;

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

I have followed and applied the conventions: but still.... sam error

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Articless extends Model {

	use HasFactory;

	protected $guarded = [];

	public function path()
	{
		return route('articles',$this);
	}

	public function user()
	{
		return $this->belongsTo(User::class);
	}
    protected static function newFactory()
{
    return ArticleFactory::new();
}

}
1 like
BrokeYourBike's avatar

I think your issue is that you calling method factory() on \App\Models\User::class. But instead you should call it like this \App\Models\User::factory().

Try to replace your code:

namespace Database\Factories;

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

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

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

Try to change

'user_id'=>factory(\App\Models\User::class),

To

'user_id' => \App\Models\User::factory(),
1 like
lemmon's avatar

@andrei.girnet96@gmail.com

change

class ArticleFactory extends Factory

to

class ArticlessFactory extends Factory

and it will work but you should follow conventions.

1 like
andreigirnet96's avatar

I get the same thing after changing

PHP Error: Class 'Articless' not found in Psy Shell code on line 1

lemmon's avatar

I remember naming a factory incorrectly using the php artisan make:factory WrongName

and I had to delete the factory and re make it with the correct name before it would work.

1 like
andreigirnet96's avatar

Nothing is working, I tried everything, I will smash my head off walls 3 hours already sitting on this banal thing

lemmon's avatar

post the entire error text.

1 like
andreigirnet96's avatar

PHP Error: Class 'Articless' not found in Psy Shell code on line 1

andreigirnet96's avatar

Thanks for all your contributions, I will just continue without this, I hope it is not very important, I can upload manually, Thank you all for your time spent with me. I hope one day I will be able to help you as you did to me.

lemmon's avatar

@andrei.girnet96@gmail.com

I have had similar problems and I just started the whole tutorial over again. I am presently following "Build a forum with tdd" on laracasts. that is laravel 5.8 and i am using 8. it is not easy but it is good practice, and it all translates over. A lot of the stuff in that collection has yet to be duplicated in laravel 6, 7 or 8 tutorials.

good luck

1 like
andreigirnet96's avatar

Finally fixed it, whoever encounters this error, I have run "composer dumpautoload", after it worked as normal. You don't imagine how happy I am, after almost one week of trying to solve this minor issue-> getting the result. Even though experienced guys will laugh on this, I am really happy now :)))))))))))))))))))))))))))))

Checkpost's avatar

Great you have it resolved already. For whoever might encounter this error

The scope resolution operator should be :: not :

This should work perfectly on tinker

This

Articless::factory()->count(10)->create();

Not this

Articless:factory()->count(10)->create();

umar-adil's avatar

I have the same issue getting. In my case I have created retailer as a new guard /middleware and running Retailer::factory()->count(100)->make(); gives me the error

Retailer::factory()->count(100)->make(); PHP Error: Call to undefined method App\Http\Middleware\Retailer::factory() in Psy Shell code on line 1

Please or to participate in this conversation.