PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 22, 2022

[FIXED] Why does my seed script for Laravel 4 generate improper insert queries?

 July 22, 2022     laravel, mysql, php, php-5.4, seeding     No comments   

Issue

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 ?


Solution

  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.)



Answered By - damiani
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing