还有我用laravel的migrate生成的数据表也一样,生成的表里面那个timestamps字段根本不起效果。是怎么回事?
这是migration文件
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() { // }}
回复内容: 为什么我设置了某个字段类型为timestamps,但是他并没有像我预期的一样在更新或者添加的时候自动填充当前时间戳?
还有我用laravel的migrate生成的数据表也一样,生成的表里面那个timestamps字段根本不起效果。是怎么回事?
这是migration文件
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() { // }}
你设置timestamp 字段 on update current_timestamp 了吗
laravel设置下默认值
$table->timestamp('updated_at')->default(\db::raw('current_timestamp on update current_timestamp'));
谢邀。
默认 eloquent 会自动维护数据库表的 created_at 和 updated_at 字段。只要把这两个「时间戳」字段加到数据库表, eloquent 就会处理剩下的工作。如果不想让 eloquent 自动维护这些字段,把下面的属性加到模型类里:
// 关闭自动更新时间戳class user extends eloquent { protected $table = 'users'; public $timestamps = false;}
你根本就没用model来做数据库的操作,和timestamps是否是current_timestamp on update current_timestamp的设置无关
laravel 是 5.1+
laravel是建议表名是复数的,你的表名应该为equips
laravel要求主键,并且名字建议为id,你的主键换名字了
要new 一个model文件
app/equip.php
这个类会自动指向equips表
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';}
操作
$e = equip::create([ 'eqiup_name' => 'wwww', 'eqiup_desc' => 'xxxx',]);$e->update([ 'eqiup_name' => 'yyyy', ]);
你如果是这么弄的,laravel仍然没有自动维护created_at updated_at
我把头给你当凳子坐,毕竟我用laravel都2年多了,不关闭timestamps,是不可能出现你的情况的