1

I am following this tutorial until I need to generate seeds using: php artisan db:seed. It always said that my Article and User class are not found.

I have looking for solution like in:

I think this should work out-of-the-box but it isn't. What is my problem? And what is my solution?

EDIT 1: Someone requested seeders codes here you are!

Article Seeder

<?php

use Illuminate\Database\Seeder;

class ArticlesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Let's truncate our existing records to start from scratch.
        Article::truncate();

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

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

User Seeder

<?php

use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Let's clear the user table first
        User::truncate();

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

        // Let's make sure everyone has the same password and
        // let's hash it before the loop, or else our seeder
        // will be too slow.
        $password = Hash::make('toptal');

        User::create([
            'name' => 'Administrator',
            'email' => 'admin@test.com',
            'password' => $password,
        ]);

        // And now let's generate a few dozen users for our app:
        for ($i = 0; $i < 10; $i ++) {
            User:;create([
                'name' => $faker->name,
                'email' => $faker->email,
                'password' => $password,
            ]);
        }
    }
}

Database Seeder

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UsersTableSeeder::class);
        $this->call(ArticlesTableSeeder::class);
    }
}
10
  • show your code. :D
    – Wreigh
    Jan 15, 2018 at 1:39
  • Nice! Which ones? Codes are scafolded from Laravel. Jan 15, 2018 at 1:41
  • the seeder, please :)
    – Wreigh
    Jan 15, 2018 at 1:41
  • There should be no problem, since the codes are generated. Nonetheless, you got it fam. Jan 15, 2018 at 1:44
  • 1
    you should use the Models fully qualified name if you're not importing it. try App\User instead of User only. or just try importing it at the top of the file: use App\User;
    – Wreigh
    Jan 15, 2018 at 1:45

3 Answers 3

2

First you should import the full class path, i.e.- App\User. Then regenerate the autoload file with- composer dump-autoload

1

You should either import the models that you've use so you can use just the Model's class name in your code or use the fully qualified name of the Model.

E.g., instead of just User, use App\User.

Use imports if you think you will have many instance where you will use the User class name, to avoid the hassle of typing the fully qualified name.

<?php
...
use App\User;
...
$users = User::all(); // <-- now you can do this.
0

I followed the same tutorial. Just add a line "use App\Article;" so that your class will find the appropriate class.
Its like including a header file path in c/c++.

1
  • Why do you suggest App\Article when the both two year old answers both suggest App\User? In which file are you suggesting this needs to be added? Jun 27, 2020 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.