2015-11-05 126 views
0

我是新来的Laravel,播种表中laravel未能5.1

我试图播种表,工匠总是返回代码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. 我的代码有什么问题? 我刚刚评论了创建线以放弃任何数据问题。 我的表“年级”存在并且是空的!打字时

错误日志:作曲家安装

> /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

Laravel日志位于'storage/logs/laravel.log'中。想到的是,您的'Grade'模型中的某些列可能不是[可填写](http://laravel.com/docs/5.1/eloquent#mass-assignment)。 – Bogdan

+0

也可以通过'Grade :: truncate()'来清空模型表的简单方法,尽管你的方法也是正确的,所以它不是错误的原因(假设表名正确拼写'Grade')。 – Bogdan

+0

Tx为您的obsversation,我没有可操作的文件。我添加了它,但它仍然不起作用:( 另外,laravel.log似乎只记录浏览器请求,我没有看到任何关于我的工匠错误 –

回答

1

有两个明显的不一致有:

  1. 与模型的create方法添加的每一列都必须是可填写的,然而id不。你也不应该传递它(除非你真的需要出于某种原因),因为它被定义为迁移中的主键,因此自动递增,因此它自己填充。所以这应该足够了Grade::create('name' => '5 Kyu', 'order' => 2]);
  2. 迁移没有定义任何时间戳列,但您的模型有protected $timestamps = true;。因此,请将$table->timestamps()添加到您的迁移中,或者在您的模型中将$timestamps设置为false

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

+0

mmm,你说的没错,我添加了时间戳,刷新了数据库,添加了id到可填充...并再次运行它仍然不适用于我:( –

+0

你说'php工匠db:seed --class = GradeSeeder'命令返回代码255,但是从我的经验来看,artisan通常在失败时返回更详细的错误你可以发布命令的确切输出吗? – Bogdan

+0

nop,不再是:> php artisan db:seed --class = GradeSeeder 过程在15:49:22以退出代码255结束 执行时间:2,320 ms –