Skip to content
Advertisement

Laravel seeder with Eloquent give timestamp null

I am trying to insert static data, with the help of the seeder and Eloquent as bellow.

use IlluminateDatabaseSeeder;
use AppRole;

class RoleSeeder extends Seeder
{
    public function run()
    {
        Role::insert(array(
            array('name' => 'admin'),
            array('name' => 'user')
        ));
    }
}

Even after using Eloquent i am getting timestamp null in database.

Advertisement

Answer

The timestamp columns (created_at and updated_at) will be assigned automatically only if you are using the Eloquent
save() method and create method are Eloquent.
While insert method is not Eloquent, it’s a query builder method. So use create method instead :

public function run()
    {
        Role::create(array(
            array('name' => 'admin'),
            array('name' => 'user')
        ));
    }

When you are using query builder, you have to created_at and updated_at value by yourself :

Role::insert(array(
  array('name' => 'admin'),
  array('name' => 'user'),
  array('created_at' => CarbonCarbon::now()),
  array('updated_at' => CarbonCarbon::now()),
));
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement