如何在Laravel的种子文件中禁用'create_at'和'update_at'?

时间:2022-10-18 21:33:11

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?

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

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

2 个解决方案

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

#2


15  

According to the Laravel docs,

根据Laravel文档,

... 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.

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

If you do not wish for Eloquent to maintain these columns, In your User model add the following:

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

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

#2


15  

According to the Laravel docs,

根据Laravel文档,

... 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.

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

If you do not wish for Eloquent to maintain these columns, In your User model add the following:

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

class User extends Eloquent {

    public $timestamps = false;

}