Juliatzin
Juliatzin

Reputation: 19695

Seeding a table fails in laravel 5.1

I'm new to Laravel,

I'm trying to seed a table, and artisan always returns code 255.

Here is my code

<?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!');
    }

Commando used

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

Here is the model:

class Grade extends Model {

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

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

}    

and here is the migration

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. Is there a way to have a log of what happens. Fixing bug with just a 255 code from Artisan is not so great!
  2. What's wrong with my code?? I just commented the creation lines to discard any data problems. My table "Grade" exists and is empty!

Error Log when typing: 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.

Upvotes: 0

Views: 2352

Answers (1)

Bogdan
Bogdan

Reputation: 44526

There are two obvious inconsistencies there:

  1. Every column added with the model's create method must be fillable, yet the id is not. Also you should not pass it at all (unless you really need to for some reason) because it's defined as the primary key in the migration, thus auto incrementing so it populates itself. So this should suffice Grade::create('name' => '5 Kyu', 'order' => 2]);
  2. The migration is not defining any timestamp columns yet your model has protected $timestamps = true;. So either add $table->timestamps() to your migration or set $timestamps to false in your model.

I've installed a clean Laravel copy and ran the migration you posted, created the model and the seeding class, and after fixing the issues listed above, running php artisan db:seed --class=GradeSeeder was done without any errors.

Upvotes: 1

Related Questions