在laravel 5.1中播种表失败

时间:2015-11-05 16:14:31

标签: laravel laravel-seeding

我是Laravel的新手,

我正在尝试播种一个表,而工匠总是返回代码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个代码修复bug并不是那么好!
  2. 我的代码出了什么问题? 我刚评论了创建行以丢弃任何数据问题。 我的表“成绩”存在并且是空的!
  3. 键入时输入错误日志: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 个答案:

答案 0 :(得分:1)

有两个明显不一致的地方:

  1. 使用模型的create方法添加的每个列都必须是可填充的,而id则不是。你根本不应该传递它(除非你真的需要由于某种原因),因为它被定义为迁移中的主键,因此自动递增以便它自己填充。所以这应该足够Grade::create('name' => '5 Kyu', 'order' => 2]);
  2. 迁移未定义任何时间戳列,但您的模型已protected $timestamps = true;。因此,要么在迁移中添加$table->timestamps(),要么在模型中将$timestamps设置为false
  3. 我安装了一个干净的Laravel副本,并运行了您发布的迁移,创建了模型和种子类,修复了上面列出的问题后,运行php artisan db:seed --class=GradeSeeder时没有任何错误。