为什么我设置了某个字段类型为timestamps,但是他并没有像我预期的一样在更新或者添加的时候自动填充当前时间戳?
还有我用Laravel的migrate生成的数据表也一样,生成的表里面那个timestamps字段根本不起效果。是怎么回事?
这是migration文件
<code><?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateEquip extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('equip', function (Blueprint $table) {
// $table->increments('id');
$table->smallInteger('equip_id')->unsigned()->comment('装备id');
$table->string('eqiup_name')->comment('装备名称');
$table->string('eqiup_desc')->comment('装备描述');
$table->timestamps();
$table->primary('equip_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
</code>为什么我设置了某个字段类型为timestamps,但是他并没有像我预期的一样在更新或者添加的时候自动填充当前时间戳?
还有我用Laravel的migrate生成的数据表也一样,生成的表里面那个timestamps字段根本不起效果。是怎么回事?
这是migration文件
<code><?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateEquip extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('equip', function (Blueprint $table) {
// $table->increments('id');
$table->smallInteger('equip_id')->unsigned()->comment('装备id');
$table->string('eqiup_name')->comment('装备名称');
$table->string('eqiup_desc')->comment('装备描述');
$table->timestamps();
$table->primary('equip_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
</code>
你设置timestamp 字段 ON UPDATE CURRENT_TIMESTAMP 了吗
laravel设置下默认值
<code> $table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));</code>
谢邀。
默认 Eloquent 会自动维护数据库表的 created_at 和 updated_at 字段。只要把这两个「时间戳」字段加到数据库表, Eloquent 就会处理剩下的工作。如果不想让 Eloquent 自动维护这些字段,把下面的属性加到模型类里:
<code>// 关闭自动更新时间戳
class User extends Eloquent {
protected $table = 'users';
public $timestamps = false;
}</code>
你根本就没用Model来做数据库的操作,和timestamps是否是CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP的设置无关
Laravel 是 5.1+
Laravel是建议表名是复数的,你的表名应该为equips
Laravel要求主键,并且名字建议为id,你的主键换名字了
要new 一个Model文件
<code>app/Equip.php</code>
这个类会自动指向equips表
<code>use Illuminate\Database\Eloquent;
class Equip extends Model
{
//这3行是管理主键的
protected $primaryKey = 'equip_id';
public $incrementing = true; //主键是自增的
protected $keyType = 'int'; //主键类型
//如果要强制指定table名,取消注释下行,不然laravel会自动找equips表
//protected $table = 'equip';
}</code>操作
<code>$e = Equip::create([
'eqiup_name' => 'wwww',
'eqiup_desc' => 'xxxx',
]);
$e->update([
'eqiup_name' => 'yyyy',
]);</code>你如果是这么弄的,Laravel仍然没有自动维护created_at updated_at
我把头给你当凳子坐,毕竟我用Laravel都2年多了,不关闭timestamps,是不可能出现你的情况的
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号