Laravel Framework 5.5.43
Server version Mysql 5.7.23-0ubuntu0.18.04.1
I use Seeder.
DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(UsersTableSeeder::class);
// factory(App\User::class, 50)->create();
}
}
UserFactory.php
$factory->define(App\User::class, function (Faker $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
];
});
$factory->defineAs(App\User::class, 'admin',function (Faker $faker) {
return [
'name' => 'user',
'email' => 'user@gmail.com',
'password' => bcrypt('123456'),
'remember_token' => str_random(10),
];
});
$factory->defineAs(App\Article::class, 'admin',function (Faker $faker) {
return [
'title' => '_title',
'slug' => '_slug',
'description_short' => '_description_short',
'description' => '_description',
'meta_title'=>'',
'meta_description'=>'',
'meta_keyword'=>'',
'published'=>'1',
'viewed'=>2,
];
});
UsersTableSeeder.php
<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
factory(App\User::class, 'admin', 1)->create()->each(function ($user){
$user->user_article()->save(factory(App\Article::class, 'admin')->make());
});
}
}
2018_06_08_112606_create_articles_table.php
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug')->unique();
$table->text('description_short')->nullable();
$table->text('description')->nullable();
$table->string('image')->nullable();
$table->boolean('image_show')->nullable();
$table->string('meta_title')->nullable();
$table->string('meta_description')->nullable();
$table->string('meta_keyword')->nullable();
$table->boolean('published');
$table->integer('viewed')->nullable();
$table->integer('created_by')->nullable();
$table->integer('modified_by')->nullable();
$table->timestamps();
});
}
2014_10_12_000000_create_users_table.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
I run the command :
php artisan migrate:refresh --seed
And get message:
Seeding: UsersTableSeeder
In Connection.php line 664:
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1970-01-01 00:00:01' for column 'created_at' at row 1 (SQL: insert into `articles` (`title`, `slug`, `description_short`, `description`,`meta_title`, `meta_description`, `meta_keyword`, `published`, `viewed`,`created_at`, `updated_at`) values (_title, title-1409181309, _description_short, _description, , , , 1, 2, 1970-01-01 00:00:01, 2018-09-14 13:09:45));
In Connection.php line 458:
SQLSTATE[22007]: Invalid datetime format: 1292 **Incorrect datetime value: '1970-01-01 00:00:01' for column 'created_at' at row 1
I can't understand why I get value 1970-01-01 00:00:01 for column 'created_at'. And how can I correct it?
Furthermore, why I get correct value 2018-09-14 13:09:45 for column 'updated_at'. Furthermore, after run command above, inserted record to User table.
I think it is strange. Any idea? Thanks.
I suggest that you change $table->timestamps();
by $table->timestamp('created_at')->useCurrent();
Change this
$factory->defineAs(App\Article::class, 'admin',function (Faker $faker) {
return [
'title' => '_title',
'slug' => '_slug',
'description_short' => '_description_short',
'description' => '_description',
'meta_title'=>'',
'meta_description'=>'',
'meta_keyword'=>'',
'published'=>'1',
'viewed'=>2,
];
to this
$factory->defineAs(App\Article::class, 'admin',function (Faker $faker) {
return [
'title' => '_title',
'slug' => '_slug',
'description_short' => '_description_short',
'description' => '_description',
'meta_title'=>'',
'meta_description'=>'',
'meta_keyword'=>'',
'published'=>'1',
'viewed'=>2,
'created_at' => $faker->dateTime(),
'updated_at' => $faker->dateTime(),
];
Same with user factory.
Update
or please check your date time format of columns in database. correct if not matching to what you are sending.
Hope this helps you.
You should set protected $dates
in your User model
class User extends Model {
// .. you code
protected $dates = [
'created_at',
'updated_at',
];
// .. you code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.