0

I have started working with Laravel 4 and am stumped by a weird issue. I wrote my migrations for the table, which successfully creates the table.

<?php    
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrgRoles extends Migration {
    protected $tableName ;

function __construct(){
    $this->tableName = "org_roles";
}
    public function up()
    {
        Schema::dropIfExists($this->tableName);
        Schema::create($this->tableName, function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('role_name')->unique();
            $table->mediumText('role_description')->nullable();
            $table->timestamps();
        });
    }

    public function down(){
        Schema::drop($this->tableName);
    }
}

Then I proceeded to write a simple model for the table as OrgRole.php

<?php

class OrgRole extends Eloquent {
  protected $tableName;
    protected $fillable = ['role_name', 'role_description'];

  function __construct(){
    $this->tableName = 'org_roles';
  }
}

Then I wrote the seed for the file as below

<?php

class OrgRolesTableSeeder extends Seeder {

  protected $tableName;

  function __construct(){
    $this->tableName = "org_roles";
  }

  public function run() {
    $defaultRoles = array(
      [
      'role_name'            =>    'guest',
      'role_description'     =>    'the most basic one of all'
      ]
    );

    foreach ( $defaultRoles as $role ) {
      OrgRole::create( $role );
    }
  }

}

Now when I run the seed command, the insert query formed is wrong, and the seeding fails

ยป php artisan db:seed                                                                                        
  [Illuminate\Database\QueryException]                                                                       
  SQLSTATE[HY000]: General error: 1364 Field 'role_name' doesn't have a default value (SQL: insert into `or  
  g_roles` (`updated_at`, `created_at`) values (2014-09-21 01:57:14, 2014-09-21 01:57:14))                   

  [PDOException]                                                                       
  SQLSTATE[HY000]: General error: 1364 Field 'role_name' doesn't have a default value  

Can somebody help me understand what am I doing wrong ?

jagzviruz
  • 1,453
  • 12
  • 27

1 Answers1

0
  1. Remove the contructors from your seeder and model files. It's unnecessary in the seeder, since you call the model OrgRole in the run() method.

  2. In the model file, replace the protected $tableName; with protected $table = 'org_roles';.

Then try running php artisan db:seed again. (Make sure you remove any records that were already seeded, or else the unique requirement on role_name will cause the seeding to fail.)

damiani
  • 7,071
  • 2
  • 23
  • 24
  • that worked.. So I was using the constructor to set the table name from a Constants Class. Why does the constructor interfere in this case ? โ€“ jagzviruz Sep 21 '14 at 03:28
  • Because your constructor in `OrgRole`, which extends the `Eloquent` class, is overriding Eloquent's (the parent) constructor. If you include a constructor in `OrgRole`, regardless of what that constructor does, you would need to include `parent::__construct` so that the `Eloquent` constructor gets called as well. Also, note that the correct variable is `$table`, not `$tableName`. โ€“ damiani Sep 21 '14 at 03:37
  • Another bonus: in your `OrgRole` model class, you don't even need to include an explicit `$table` declaration at all, since you have named your db file `org_roles`. Laravel will automatically figure out the table name from your model name, assuming [a] your table name is the plural of your model name, and [b] capital letters within your model name correspond to an underscore in your table name. โ€“ damiani Sep 21 '14 at 03:46