Running the factory using _factory(App\User::class)->create()_ resulted invalid SQL with following error:
Illuminate\Database\QueryException with message 'SQLSTATE[42703]: Undefined column: 7 ERROR: column "updated_at" of relation "employees" does not exist
LINE 1: ... "gender", "position_id", "employment_status_id", "updated_a...
^ (SQL: insert into "employees" ("first_name", "middle_name", "last_name", "gender", "position_id", "employment_status_id", "updated_at", "created_at") values (Reid, Howell, Weissnat, male, 3, 2, 2017-02-02 14:24:22, 2017-02-02 14:24:22) returning "employee_id")'
SHOULD NOT CAUSE ERROR SINCE I INTEND TO EXCLUDE _$table->timestamps()_ (updated_at and created_at columns).
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('employee_id')->unique();
$table->string('username')->unique();
$table->string('password');
$table->rememberToken();
});
php artisan make:model -m EmployeeSchema::create('employees', function (Blueprint $table) {
$table->increments('employee_id');
$table->string('first_name');
$table->string('middle_name');
$table->string('last_name');
$table->enum('gender', ['male', 'female']);
$table->date('date_employed')->default('now()');
$table->integer('position_id')->default(1);
$table->integer('employment_status_id')->default(1);
$table->unique(['first_name', 'middle_name', 'last_name'], 'name');
});
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'username' => $faker->userName,
'employee_id' => function(){
return \factory(\App\Employee::class)->create()->employee_id;
},
'password' => bcrypt('secret'),
'remember_token' => str_random(10),
];
});
$factory->define(App\Employee::class, function (\Faker\Generator $faker) {
$genders = ['male', 'female'];
$gender = $genders[\random_int(0, 1)];
return [
'first_name' => ('male' === $gender) ? $faker->firstNameMale : $faker->firstNameFemale,
'middle_name' => $faker->lastName,
'last_name' => $faker->lastName,
'gender' => $gender,
'position_id' => \random_int(1, \App\Position::all()->count()),
'employment_status_id' => \random_int(1, 3)
];
});
php artisan tinkerfactory(App\User::class)->create();Illuminate\Database\QueryException with message 'SQLSTATE[42703]: Undefined column: 7 ERROR: column "updated_at" of relation "employees" does not exist
LINE 1: ... "gender", "position_id", "employment_status_id", "updated_a...
^ (SQL: insert into "employees" ("first_name", "middle_name", "last_name", "gender", "position_id", "employment_status_id", "updated_at", "created_at") values (Reid, Howell, Weissnat, male, 3, 2, 2017-02-02 14:24:22, 2017-02-02 14:24:22) returning "employee_id")'
@gabbydgab The reason you are seeing the error is because you haven't disabled timestamps for App\Employee & App\User model. You need to disable these in your models like this:
public $timestamps = false;
What is timestamp here.. why we are dealing with!
Any honest !
Cheer!
By default, Eloquent will maintain the created_at and updated_at columns on your database table automatically. Simply add these timestamp columns to your table and Eloquent will take care of the rest. If you do not wish for Eloquent to maintain these columns, add the following property to your model:
Most helpful comment
@gabbydgab The reason you are seeing the error is because you haven't disabled timestamps for
App\Employee&App\Usermodel. You need to disable these in your models like this: