Seeder command not working. Laravel 5.4

I am new to laravel. I am trying to follow the tutorial in which I am creating a database planter for db. So for this I used the commandphp artisan make:seeder ArticlesTableSeeder

Seeder

class ArticlesTableSeeder extends Seeder
{
    public function run()
    {
        // Let truncate our existing records to start from scratch.
        Article::truncate();

        $faker = \Faker\Factory::create();

        // And now, let create a few articles in our database:
        for ($i = 0; $i < 50; $i++) {
            Article::create([
                'title' => $faker->sentence,
                'body' => $faker->paragraph,
            ]);
        }
    }
}

      

Now when I run the command php artisan db:seed --class=ArticlesTableSeeder

I have this error

[Symfony\Component\Debug\Exception\FatalThrowableError] Class 'Article' not found

I have a modal article

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
   protected $fillable = ['title', 'body'];
}

      

What am I doing wrong here? I searched for this error and found that most of them were caused by spelling errors. I think I have no spelling error. What's wrong with this code?

Help will be highly appreciated.

+3


source to share


3 answers


use App\Article;

class ArticlesTableSeeder extends Seeder
    {
        public function run()
        {
            // Let truncate our existing records to start from scratch.
            Article::truncate();

            $faker = \Faker\Factory::create();

            // And now, let create a few articles in our database:
            for ($i = 0; $i < 50; $i++) {
                Article::create([
                    'title' => $faker->sentence,
                    'body' => $faker->paragraph,
                ]);
            }
        }
    }

      



0


source


You missed to include your model in your controller, so what do you get class not found error

. Add this line to the controller before the class



use App\Article;

      

0


source


All you have to do is add \App\

before Article

or just adduse \App\Article

class ArticlesTableSeeder extends Seeder
{
    public function run()
    {
        // Let truncate our existing records to start from scratch.
        \App\Article::truncate();

        $faker = \Faker\Factory::create();

        // And now, let create a few articles in our database:
        for ($i = 0; $i < 50; $i++) {
            \App\Article::create([
                'title' => $faker->sentence,
                'body' => $faker->paragraph,
            ]);
        }
    }
}

      

-1


source







All Articles