我正在将我们的一个web应用程序从CodeIgniter转换为Laravel。然而,此时我们不想将updated_at / created_at字段添加到我们所有的表中,因为我们已经有一个日志类,它已经为我们更深入地完成了所有这些工作。

我知道我可以设置$timestamps = false;:

Vendor\laravel\framework\src\illuminate\Datebase\Eloquent\Model.php

然而,我宁愿不改变Laravel的核心文件,或者让我的每个模型都在顶部。有没有办法在其他地方禁用所有模型?


当前回答

只要将Model中的公共时间戳变量声明为false,一切都将正常工作。

公共$timestamps = false;

其他回答

只要将Model中的公共时间戳变量声明为false,一切都将正常工作。

公共$timestamps = false;

雄辩的模型:

class User extends Model    
{      
    protected $table = 'users';

    public $timestamps = false;
}

或者简单地试试这个

$users = new Users();
$users->timestamps = false;
$users->name = 'John Doe';
$users->email = 'johndoe@example.com';
$users->save();

重写模型中的setUpdatedAt()和getUpdatedAtColumn()函数

public function setUpdatedAt($value)
{
   //Do-nothing
}

public function getUpdatedAtColumn()
{
    //Do-nothing
}

将这一行添加到您的模型中:

将现有变量$timestamps的true重写为false

/**
 * Indicates if the model should be timestamped.
 *
 * @var bool
 */

public $timestamps = false;

您可以暂时禁用时间戳

$timestamps = $user->timestamps;
$user->timestamps=false;   // avoid view updating the timestamp

$user->last_logged_in_at = now();
$user->save();

$user->timestamps=$timestamps;   // restore timestamps