Home » Php » php – How to disable 'create_at' and 'update_at' in Laravel's seed file?

php – How to disable 'create_at' and 'update_at' in Laravel's seed file?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I don’t want to use rows ‘update_at’ and ‘create_at’, but Laravel’s seed file is trying to update it. How can I disable it?

Here is the code that I’m using:

use Illuminate\Database\Migrations\Migration;

class SeedUsersTable extends Seeder {

// $timestamps = false;  <=== will return error
// public static $timestamps = false;  <=== will return error

    public function run()
    {
        DB::table('users')->delete();
        User::create(array(
                'id' => 1,
                'name' => 'Админ',
                'password' => Hash::make('admin'),
                'login' => 'admin'
        ));
    }
}
How to&Answers:
use Illuminate\Database\Migrations\Migration;

class SeedUsersTable extends Seeder {

    public function run()
    {
        DB::table('users')->delete();

        $user = new User(array(
                'id' => 1,
                'name' => 'Админ',
                'password' => Hash::make('admin'),
                'login' => 'admin'
        ));

        $user->timestamps = false;
        $user->save();
    }
}

Answer:

According to the Laravel docs,

… 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, In your User model add the following:

class User extends Eloquent {

    public $timestamps = false;

}