在 laravel 5.1 中播种 table 失败

Seeding a table fails in laravel 5.1

我是 Laravel、

的新手

我正在尝试播种 table,并且 artisan 总是 returns 代码 255。

这是我的代码

<?php

use App\Grade;
use Illuminate\Database\Seeder;

class GradeSeeder extends Seeder {

    public function run()
    {
        //This doesn't even work
        DB::table('Grade')->delete();
//      Grade::create(['id' => '1','name' => "5 Kyu",'order' => 2]);
    }
}

DatabaseSeeder.php

class DatabaseSeeder extends Seeder {

    public function run()
    {
        Model::unguard();
        //Seed the countries
        $this->call('CountriesSeeder');
        $this->command->info('Seeded the countries!');
        $this->call('GradeSeeder');
        $this->command->info('Seeded the grades!');
    }

使用突击队

php artisan db:seed --class=GradeSeeder
or
php artisan db:seed // In this case seeding countries works but mine don't

这是模型:

class Grade extends Model {

    protected $table = 'Grade';
    public $timestamps = true;

    protected $fillable = [
        'name',
        'order'
    ];

}    

这是迁移

class CreateGradeTable extends Migration {

public function up()
{
    Schema::create('Grade', function(Blueprint $table) {
        $table->increments('id');
        $table->string("name")->unique();
        $table->tinyInteger("order");

    });
}

public function down()
{
    Schema::drop('Grade');
}
}  
  1. 有没有办法记录发生的事情。仅使用来自 Artisan 的 255 代码修复错误并不是很好!
  2. 我的代码有什么问题?? 我刚刚评论了创建行以丢弃任何数据问题。 我的 table "Grade" 存在并且是空的!

键入时的错误日志:composer install

> /usr/local/bin/composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
> php artisan clear-compiled

Warning: require(/Applications/XAMPP/xamppfiles/htdocs/kendo/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /    Applications/XAMPP/xamppfiles/htdocs/kendo/bootstrap/autoload.php on line 17

Fatal error: require(): Failed opening required '/Applications/XAMPP/xamppfiles/htdocs/kendo/bootstrap/../vendor/autoload.php' (include_path='.:') in /    Applications/XAMPP/xamppfiles/htdocs/kendo/bootstrap/autoload.php on line 17
Script php artisan clear-compiled handling the post-install-cmd event returned with an error



  [RuntimeException]  
  Error Output:       



install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-plugins] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--no-    progress] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--ignore-platform-reqs] [--] [<packages>]...



Process finished with exit code 255 at 19:51:06.
Execution time: 941 ms.

这里有两个明显的不一致:

  1. 使用模型的 create 方法添加的每一列都必须是可填充的,但 id 不是。此外,您根本不应该传递它(除非出于某种原因确实需要),因为它被定义为迁移中的主键,因此会自动递增,因此它会自行填充。所以这应该足够 Grade::create('name' => '5 Kyu', 'order' => 2]);
  2. 迁移未定义任何时间戳列,但您的模型有 protected $timestamps = true;。因此,要么将 $table->timestamps() 添加到您的迁移中,要么在您的模型中将 $timestamps 设置为 false

我已经安装了一个干净的 Laravel 副本和 运行 您发布的迁移,创建了模型和播种 class,并在解决上面列出的问题后,运行 php artisan db:seed --class=GradeSeeder 没有出现任何错误。