如何禁用' create_at'和' update_at'在Laravel的种子档案中?

时间:2013-12-29 08:04:54

标签: php mysql laravel-4

我不想使用行'updated_at'和'created_at',但是Laravel种子文件正在尝试更新它。我该如何禁用它?

以下是我正在使用的代码:

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'
        ));
    }
}

2 个答案:

答案 0 :(得分:16)

根据Laravel文档,

  

...默认情况下,Eloquent会自动维护数据库表中的created_at和updated_at列。只需将这些时间戳列添加到您的表中,Eloquent将负责其余的工作。

如果您不希望Eloquent维护这些列,请在User模型中添加以下内容:

class User extends Eloquent {

    public $timestamps = false;

}

答案 1 :(得分:4)

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();
    }
}