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

laravel dev's avatar

laravel:8 PHP Error: Class 'App\Article' not found in \..vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php on line 625

M TRYING TO make articles using tinker but it gives this error: "PHP Error: Class 'App\Article' not found in ...\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php on line 625" i use this command to make it:

  Article::factory()->count(5)->create();

Article.php:

  <?php

namespace App\Models;

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

class Article extends Model
{
      use HasFactory;

    // protected $fillable = ['title', 'excerpt', 'body'];
    protected $guarded = [];

    public function user()
    {
        //
    }
}

database>factories>ArticleFactory.php:

  <?php

namespace Database\Factories;

use App\Article;
use Illuminate\Database\Eloquent\Factories\Factory;

class ArticleFactory extends Factory
{

protected $model = Article::class;


public function definition()
{
    return [
        'user_id' => factory(\App\User::class),
        'title' => $faker->sentence,
        'excerpt' => $faker->sentence,
        'body' => $faker->paragraph
    ];
}
}

when i google error the i found to use to run "composer dump" and after running this command now it gives:

[!] Aliasing 'Article' to 'App\Models\Article' for this Tinker session.
0 likes
6 replies
npispas's avatar

Hi @laravel dev, your Article Factory is using the wrong namespace of the model Article.

You should replace App\Article with App\Models\Article.

1 like
laravel dev's avatar

i replace it but now it gave a new error:

  [!] Aliasing 'Article' to 'App\Models\Article' for this Tinker session.
  PHP Error:  Call to undefined function Database\Factories\factory() in\database\factories\ArticleFactory.php on line 25

ArticleFactory.php

<?php

namespace Database\Factories;

use App\Models\Article;
use Illuminate\Database\Eloquent\Factories\Factory;

class ArticleFactory extends Factory
{ ...
npispas's avatar

That's true because if you check line 25 you are using factory(\App\User::class). The factory method is not defined in this context.

Therefore you need to replace it with \App\Models\User::factory()->create()->id

1 like
laravel dev's avatar

now it need to change other lines also:

      <warning>PHP Notice:  Undefined variable: faker in \database\factories\ArticleFactory.php on line  
  26</warning>
  <warning>PHP Notice:  Trying to get property 'sentence' of non-object in \database\factories 
  \ArticleFactory.php on line 26</warning><warning>PHP Notice:  Undefined variable: faker in \database  
 \factories\ArticleFactory.php on line 27</warning>
<warning>PHP Notice:  Trying to get property 'sentence' of non-object in \database\factories  
\ArticleFactory.php on line 27</warning><warning>PHP Notice:  Undefined variable: faker in \database  
\factories\ArticleFactory.php on line 28</warning>
<warning>PHP Notice:  Trying to get property 'paragraph' of non-object in \database\factories  
\ArticleFactory.php on line 28</warning>

  Illuminate\Database\QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 
  Column 'title' cannot be null (SQL: insert into `articles` (`user_id`, `title`, `excerpt`, `body`, 
 `updated_at`, `created_at`) values (8, ?, ?, ?, 2021-02-27 13:29:16, 2021-02-27 13:29:16))'

Is there any series same like this for laravel:8 because i am following laravel 6 series.

npispas's avatar
npispas
Best Answer
Level 2

You need to reshape your code a bit.

The problem above is that $faker is not available in this context. You will need to add $this keyword in order to obtain a reference to faker.

Your code shall look as follows:

<?php

namespace Database\Factories;

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

class ArticleFactory extends Factory
{

   protected $model = Article::class;

   public function definition()
   {
       return [
           'user_id' => User::factory()->create()->id,
           'title' => $this->faker->sentence,
           'excerpt' => $this->faker->sentence,
           'body' => $this->faker->paragraph
       ];
   }
}

Yes, there are a lot of series which you can watch. I would start with my personal favourite Jeffrey Way: https://laracasts.com/series/laravel-6-from-scratch

Snapey's avatar

Your factory class should have access to the faker library under $this->faker so amend your code like;

        'title' => $this->faker->sentence,
        'excerpt' => $this->faker->sentence,
        'body' => $this->faker->paragraph
1 like

Please or to participate in this conversation.