數(shù)據(jù)渲染到模板經(jīng)常用到日期格式。而數(shù)據(jù)庫一般保存時間戳。每次更新或查詢都要做轉(zhuǎn)換。
使用Eloquent 自動轉(zhuǎn)換
?php namespace App\Model; use Illuminate\Database\Eloquent\Model; class Goods extends Model { public function setStartTimeAttribute($value) { $this->attributes['start_time'] = is_int($value) ? $value : strtotime($value); } public function getStartTimeAttribute() { return date('Y-m-d H:i:s', $this->attributes['start_time']); } }
方法名稱應(yīng)與被轉(zhuǎn)換字段名稱相同
以上這篇laravel 時間格式轉(zhuǎn)時間戳的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。