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 ?