Laravel 5.1中的数据库填充

4

我正在使用laravel 5.1。我试图运行数据库填充命令。

我的表名是users

我的迁移文件如下:

2015_11_09_194832_create_users_table.php

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
         Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

DatabaseSeeder.php

<?php

use App\User;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();
        $this->call(UsersTableSeeder::class);
        Model::reguard();
    }
}

UsersTableSeeder.php

<?php

// DatabaseSeeder.php
use App\User;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;



class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();
        DB::table('users');
        $users = array(
                ['name' => 'Ryan Chenkie', 'email' => 'ryanchenkie@gmail.com', 'password' => Hash::make('secret')],
                ['name' => 'Chris Sevilleja', 'email' => 'chris@scotch.io', 'password' => Hash::make('secret')],
                ['name' => 'Holly Lloyd', 'email' => 'holly@scotch.io', 'password' => Hash::make('secret')],
                ['name' => 'Adnan Kukic', 'email' => 'adnan@scotch.io', 'password' => Hash::make('secret')],
        );

        // Loop through each user above and create the record for them in the database
        foreach ($users as $user)
        {
            User::create($user);
        }
        Model::reguard();
    }
}

当我尝试运行填充命令 php artisan db:seed 时,我遇到以下错误。
  [ReflectionException]
  Class UsersTableSeeder does not exist

有人能在这方面帮助我吗?涉及IT技术。

1
这两个类在同一个全局命名空间中,因此不需要使用"use"。尝试运行“composer dump-autoload” - 它应该重新加载播种机列表。 - jedrzej.kurylo
1
创建新的种子后,您应该在项目根目录中运行 composer dumpautoload - Alex
1
就像上面的两位用户已经评论过的那样,您需要在项目根目录中使用 composer dump-autoload。您可以在终端中使用相应的命令。 - Narendrasingh Sisodia
1个回答

2
我遇到过几次这个问题,当我要在新项目中添加种子或者向现有项目中添加一些数据以进行测试时。
jedrzej.kurylo和Alex都走在了正确的轨道上,使用"composer dump-autoload"命令将重新生成你的自动加载文件,并包括你刚刚添加的“seeder”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接