Laravel Seeding missing classes

為{幸葍}努か 提交于 2021-01-28 08:21:04

问题


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:

  • https://laracasts.com/discuss/channels/lumen/unable-to-run-php-artisan-dbseed-due-to-missing-class (setting up composer.json's auto load paths and composer dump-autoload)
  • Laravel cannot find class of a package
  • I have deleting my vendor folder and do composer install again
  • Also importing the file manually, require_once with relative path to the model from the seeding or root of the projet, but neither works.

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);
    }
}

回答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.



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/48255953/laravel-seeding-missing-classes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!