我无法在 laravel 中播种数据

I cant seed data in laravel

H有2个table,用户和作者我都有各自的模型。但是当我尝试播种时出现以下错误:

Class 'Author' not found in /var/www/html/shopping/database/seeds/AuthorsTableSeeder.php on line 12

[Symfony\Component\Debug\Exception\FatalErrorException] Class 'Author' not found

早些时候我的用户遇到了同样的问题 table 但我通过添加 use Illuminate\Foundation\Auth\User;

修复了它

这是我的作者seeder.php

<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;


Class AuthorsTableSeeder extends Seeder {

    public function run()
    {
DB::table('authors')->delete();

        Author::create(array(
            'name' => 'Lauren',
            'surname'=>'Oliver'
        ));

        Author::create(array(
            'name' => 'Stephenie',
            'surname'=>'Meyer'
        ));

        Author::create(array(
            'name' => 'Dan',
            'surname'=>'Brown'
        ));

    }

}

我的数据库seeder.php低于

<?php

use Illuminate\Database\Seeder;


class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
    Eloquent::unguard();

    $this->call('UsersTableSeeder');
    $this->command->info('Users table seeded!');
    $this->call('AuthorsTableSeeder');
    $this->command->info('Authors table seeded!');

    }
}

还有我的website folder。

AuthorsTableSeeder 添加模型名称 Author

use App\Author;

因为,您使用的是 Author 模型,没有在顶部添加。

你应该试试这个:

<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;


Class AuthorsTableSeeder extends Seeder {

    public function run()
    {
        DB::table('authors')->delete();

        DB::table('authors')->insert(array(
            'name' => 'Lauren',
            'surname'=>'Oliver'
        ));

        DB::table('authors')->insert(array(
            'name' => 'Stephenie',
            'surname'=>'Meyer'
        ));

        DB::table('authors')->insert(array(
            'name' => 'Dan',
            'surname'=>'Brown'
        ));

    }

}

希望这对你有用!!!

我找到了答案。问题出在语法上。我使用了以下并且有效

<?php namespace App;
use Illuminate\Database\Eloquent\Model;

Class Author extends Model {

protected $fillable = array('name','surname');
protected $table = 'authors';



}

我替换了以下代码的位置 受保护的 $fillable = array('name','surname'); 保护 $table = 'authors';

我用 'model' 而不是 'eloquent' 并且有效