notalentgeek
notalentgeek

Reputation: 5789

Laravel Seeding missing classes

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' => '[email protected]',
            '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);
    }
}

Upvotes: 1

Views: 5859

Answers (3)

Gagan Adlakha
Gagan Adlakha

Reputation: 1

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++.

Upvotes: 0

Nuruzzaman Milon
Nuruzzaman Milon

Reputation: 312

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

Upvotes: 2

Wreigh
Wreigh

Reputation: 3297

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.

Upvotes: 1

Related Questions