【问题标题】:Seeding a table fails in laravel 5.1在 laravel 5.1 中播种表失败
【发布时间】:2015-11-06 09:52:54
【问题描述】:

我是 Laravel 的新手,

我正在尝试为一张桌子播种,而 artisan 总是返回代码 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. 我的代码有什么问题?? 我只是评论了创建行以丢弃任何数据问题。 我的“等级”表存在但为空!

输入错误日志: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.

【问题讨论】:

  • Laravel 日志位于storage/logs/laravel.log。想到的事情是您的Grade 模型的某些列可能不是fillable
  • 另外,清空模型表的更简单方法是使用Grade::truncate(),尽管您的方式也是正确的,所以这不是错误的原因(假设表名拼写正确Grade )。
  • Tx 供您参考,我没有可填写的内容。我添加了它,但它仍然不起作用:(另外,laravel.log 似乎只记录浏览器请求,我没有看到任何关于我的工匠错误的信息
  • 您能否发布database/seeds/DatabaseSeeder.php 的内容以及您正在运行的确切播种命令?
  • 将 DatabaseSeeder、Model 和 Migration 添加到问题中

标签: laravel laravel-seeding


【解决方案1】:

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

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

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

【讨论】:

  • 嗯。你说的对。我添加了时间戳,刷新数据库,将 id 添加到可填充...并再次运行它仍然对我不起作用:(
  • 您说php artisan db:seed --class=GradeSeeder 命令返回代码255,但根据我的经验,artisan 通常在失败时返回更详细的错误。您能否发布命令的确切输出?
  • nop, no more: > php artisan db:seed --class=GradeSeeder 进程在 15:49:22 以退出代码 255 结束。执行时间:2,320 毫秒。
  • 你的 Laravel 应用是否启用了调试模式?能否请您指定您的操作系统和 PHP 版本。
  • 调试已开启,Mac OS Yosemite,PHP 版本 5.6.12,本地环境。
猜你喜欢
  • 2015-09-13
  • 2015-09-27
  • 2015-09-16
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多